SpringBoot快速入门
Spirng boot 入门引导部分
关于Spring Boot 百度百科是这么介绍的:
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
与其它框架不同,SpirngBoot并不能称之为一个框架,而是针对spirng在整合各种技术的配置繁琐性进行统一的管理配置。其开发技术还是使用spring,springmvc等等技术并没有多大改变。
Spring Boot设计的目的是让您尽可能快地启动和运行,并尽量减少前期的开销 。
Spring Boot 为了简化开发 大概分为了以下几个部分:
-
统一的版本管理
使用spirng boot开发 前提必须指定父类依赖,具体如下:
maven版本
1 2 3 4 5 |
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> |
Gradle版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
buildscript { repositories { jcenter() maven { url 'https://repo.spring.io/snapshot' } maven { url 'https://repo.spring.io/milestone' } } dependencies { classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.1.0.BUILD-SNAPSHOT' } } apply plugin: 'java' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' |
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
- 集成的依赖管理
Spring Boot为我们们提供了简化企业级开发绝大多数场景的 starter pom,只要使用了应用场景所需要的 starter pom,相相关的技术配置将会消除,就可以得到 Spring Boot为我们提供的自动配置的Bean。简单的说,就是你想进行什么样的应用开发,导入指定的starter依赖即可。
比如,如果我们想使用 spring web开发就可以导入依赖:
1 2 3 4 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> |
这个依赖提过了基于web的开发的基本配置,并且内部默认集成了tomcat
具体请查看这篇博客https://www.cnblogs.com/EasonJim/p/7615801.html
-
Spring Boot application starters
来源:https://blog.csdn.net/u010919351/article/details/80238579
Name | Description | 备注 |
---|---|---|
spring-boot-starter-thymeleaf | 使MVC Web applications 支持Thymeleaf | Thymeleaf是一个JAVA库,一个XML/XHTML/HTML5的可扩展的模板引擎,同类事物:Jsp |
spring-boot-starter-data-couchbase | 使用Couchbase 文件存储数据库、Spring Data Couchbase | Spring Data是一个用于简化数据库访问,并支持云服务的开源框架 |
spring-boot-starter-artemis | 为JMS messaging使用Apache Artemis | JMS是Java消息服务;HornetQ代码库捐献给 Apache ActiveMQ 社区,它现在成为ActiveMQ旗下的一个子项目,名为 “Artemis” |
spring-boot-starter-web-services | 使用Spring Web Services | Spring Web Services是基于Spring框架的Web服务框架,主要侧重于基于文档驱动的Web服务,提供SOAP服务开发,允许通过多种方式创建 Web 服务。 |
spring-boot-starter-mail | 使用Java Mail、Spring email发送支持 | Java Mail、Spring email为邮件发送工具 |
spring-boot-starter-data-redis | 通过Spring Data Redis 、Jedis client使用Redis键值存储数据库 | Jedis 是 Redis 官方首选的 Java 客户端开发包 |
spring-boot-starter-web | 构建Web,包含RESTful风格框架SpringMVC和默认的嵌入式容器Tomcat | RESTful是一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件 |
spring-boot-starter-activemq | 为JMS使用Apache ActiveMQ | ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线 |
spring-boot-starter-data-elasticsearch | 使用Elasticsearch、analytics engine、Spring Data Elasticsearch | ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口 |
spring-boot-starter-integration | 使用Spring Integration | Spring Integration是Spring框架创建的一个API,面向企业应用集成(EAI) |
spring-boot-starter-test | 测试 Spring Boot applications包含JUnit、 Hamcrest、Mockito | JUnit、 Hamcrest、Mockito为测试框架 |
spring-boot-starter-jdbc | 通过 Tomcat JDBC 连接池使用JDBC | |
spring-boot-starter-mobile | 通过Spring Mobile构建Web应用 | Spring Mobile 是 Spring MVC 的扩展,用来简化手机上的Web应用开发 |
spring-boot-starter-validation | 通过Hibernate Validator使用 Java Bean Validation | Bean Validation 是一个数据验证的规范;Hibernate Validator是一个数据验证框架 |
spring-boot-starter-hateoas | 使用Spring MVC、Spring HATEOAS构建 hypermedia-based RESTful Web 应用 | hypermedia-based似乎是专业术语,博主表示不会翻译;Spring HATEOAS 是一个用于支持实现超文本驱动的 REST Web 服务的开发库 |
spring-boot-starter-jersey | 通过 JAX-RS、Jersey构建 RESTful web applications;spring-boot-starter-web的另一替代方案 | JAX-RS是JAVA EE6 引入的一个新技术;Jersey不仅仅是一个JAX-RS的参考实现,Jersey提供自己的API,其API继承自JAX-RS,提供更多的特性和功能以进一步简化RESTful service和客户端的开发 |
spring-boot-starter-data-neo4j | 使用Neo4j图形数据库、Spring Data Neo4j | Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中 |
spring-boot-starter-websocket | 使用Spring WebSocket构建 WebSocket 应用 | Websocket是一个持久化的协议,相对于HTTP这种非持久的协议来说 |
spring-boot-starter-aop | 通过Spring AOP、AspectJ面向切面编程 | AspectJ是一个面向切面的框架,它扩展了Java语言 |
spring-boot-starter-amqp | 使用Spring AMQP、Rabbit MQ | Spring AMQP 是基于 Spring 框架的 AMQP 消息解决方案,提供模板化的发送和接收消息的抽象层,提供基于消息驱动的 POJO;RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统 |
spring-boot-starter-data-cassandra | 使用Cassandra分布式数据库、Spring Data Cassandra | Apache Cassandra是一套开源分布式NoSQL数据库系统 |
spring-boot-starter-social-facebook | 使用 Spring Social Facebook | Facebook提供用户使用第三方社交网络的账号API,同类事物:QQ第三方登录接口 |
spring-boot-starter-jta-atomikos | 为 JTA 使用 Atomikos | JTA,即Java Transaction API,JTA允许应用程序执行分布式事务处理;Atomikos 是一个为Java平台提供增值服务的并且开源类事务管理 |
spring-boot-starter-security | 使用 Spring Security | Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架 |
spring-boot-starter-mustache | 使MVC Web applications 支持Mustache | Mustache是基于JavaScript实现的模版引擎,类似于jQuery Template,但是这个模版更加的轻量级,语法更加的简单易用,很容易上手 |
spring-boot-starter-data-jpa | 通过 Hibernate 使用 Spring Data JPA (Spring-data-jpa依赖于Hibernate) | JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中 |
spring-boot-starter | Core starter,包括 自动配置支持、 logging and YAML | logging是指的Starter的专有框架;YAML是“另一种标记语言”的外语缩写,它参考了其他多种语言,包括:XML、C语言、Python、Perl以及电子邮件格式RFC2822 |
spring-boot-starter-groovy-templates | 使MVC Web applications 支持Groovy Templates | Groovy Templates是模视图模板,同类事物:Jsp |
spring-boot-starter-freemarker | 使MVC Web applications 支持 FreeMarker | FreeMarker是模视图模板,同类事物:Jsp |
spring-boot-starter-batch | 使用Spring Batch | Spring Batch是一个轻量级的,完全面向Spring的批处理框架,可以应用于企业级大量的数据处理系统 |
spring-boot-starter-social-linkedin | 使用Spring Social LinkedIn | LinkedIn提供用户使用第三方社交网络的账号API,同类事物:QQ第三方登录接口 |
spring-boot-starter-cache | 使用 Spring caching 支持 | Spring caching是Spring的提供的缓存框架 |
spring-boot-starter-data-solr | 通过 Spring Data Solr 使用 Apache Solr | Apache Solr 是一个开源的搜索服务器。Solr 使用 Java 语言开发,主要基于 HTTP 和 Apache Lucene 实现 |
spring-boot-starter-data-mongodb | 使用 MongoDB 文件存储数据库、Spring Data MongoDB | Spring Data是一个用于简化数据库访问,并支持云服务的开源框架 |
spring-boot-starter-jooq | 使用JOOQ链接SQL数据库;spring-boot-starter-data-jpa、spring-boot-starter-jdbc的另一替代方案 | jOOQ(Java Object Oriented Querying,即面向Java对象查询)是一个高效地合并了复杂SQL、类型安全、源码生成、ActiveRecord、存储过程以及高级数据类型的Java API的类库。 |
spring-boot-starter-jta-narayana | Spring Boot Narayana JTA Starter | 似乎和jboss.narayana.jta有关 |
spring-boot-starter-cloud-connectors | 用连接简化的 Spring Cloud 连接器进行云服务就像Cloud Foundry、Heroku那样 | Cloud Foundry是VMware推出的业界第一个开源PaaS云平台;Heroku是一个支持多种编程语言的云平台即服务 |
spring-boot-starter-jta-bitronix | 为JTA transactions 使用 Bitronix | Bitronix Transaction Manager (BTM) 是一个简单但完整实现了 JTA 1.1 API 的类库,完全支持 XA 事务管理器,提供 JTA API 所需的所有服务,并让代码保持简洁 |
spring-boot-starter-social-twitter | 使用 Spring Social Twitter | Twitter提供用户使用第三方社交网络的账号API,同类事物:QQ第三方登录接口 |
spring-boot-starter-data-rest | 使用Spring Data REST 以 REST 方式暴露 Spring Data repositories | 博主也不是很明白。原文:exposing Spring Data repositories over REST using Spring Data REST |
- 单独的spirng应用
Spirng boot 的开发和启动入口
1 2 3 4 5 6 |
@SpringBootApplication public class SpringbootDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootDemoApplication.class, args); } } |
@SpringBootApplication 说明这是一个标准的spring入口类,spring 所有业务将从这个main方法作为入口进行操作。您可以将其打成一个jar文件作为独立的应用 运行。
- 与其他技术结合的自动配置
进入@SpringBootApplication,我们会看到其内部集成了很多注解,其中就包括了自动配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
@Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )} @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} ), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication { ... ... } |
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
... ...
}
-
统一的配置管理
如果你想要对自动的配置进行修改,则需要创建配置文件
,其详细的配置你可以在官网查到https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/reference/htmlsingle/#common-application-properties配置文件有两种,任选其一:
下面名称是默认的文件名称,如果你想用自定义的名称,需要进行配置指定才可以
- application.properties
1 2 3 |
spring.profiles.active=dev spring.datasource.data-username=root spring.datasource.data-password=root |
- application.yml
yml文件格式语法参考:http://www.ruanyifeng.com/blog/2016/07/yaml.html?f=tt
1 2 3 4 5 6 7 8 |
spring: profiles: active: prod datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test username: root password: root |
Spring Boot 入门案例:
如果你看了上面的引导部分,你可能知道进行Spirng 开发一共分为三块:
- 指定spirng boot父类依赖
- 导入指定的 starter pom 依赖。(当然,您可以不用Spring boot的集成starter pom依赖)
- 创建入口
Spring boot 入门案例(一)
文件结构
需要注意,controller包不许在入口类中,因为入口类默认扫描本包及其子包。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
│ pom.xml └─src ├─main │ ├─java │ │ └─com │ │ └─lifeibai │ │ └─springboot │ │ │ SpringbootDemoApplication.java │ │ │ │ │ └─controller │ │ HelloWorldController.java │ │ │ ├─resources │ └─webapp │ index.html │ └─test └─java |
SpringbootDemoApplication.java
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.lifeibai.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootDemoApplication.class, args); } } |
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
HelloWorldController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.lifeibai.springboot.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; /** * Author:Alan * * @Date: 2018-06-12 12:43:05 * 简单的web程序 */ @Controller public class HelloWorldController { @GetMapping("/hello") public String hello(){ return "index.html"; } } |
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
/**
* Author:Alan
*
* @Date: 2018-06-12 12:43:05
* 简单的web程序
*/
@Controller
public class HelloWorldController {
@GetMapping("/hello")
public String hello(){
return "index.html";
}
}
index.html
1 2 3 4 5 6 7 8 9 10 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Spring boot 入门案例(一)</title> </head> <body> <h1>Hello,Spirng Boot!!!</h1> </body> </html> |
访问:
spring boot默认的tomcat服务器,其默认8080端口,如果你想更改端口,需要创建application.properties中配置:server.port=80
Spring boot 入门案例(二)--通过Spring Initializr快速创建spirng boot应用。
点击Generater Project会下载文件:
解压文件,其文件结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
C:. │ .gitignore │ mvnw │ mvnw.cmd │ pom.xml │ ├─.mvn │ └─wrapper │ maven-wrapper.jar │ maven-wrapper.properties │ └─src ├─main │ ├─java │ │ └─com │ │ └─example │ │ └─demo │ │ DemoApplication.java │ │ │ └─resources │ │ application.properties │ │ │ ├─static │ └─templates └─test └─java └─com └─example └─demo DemoApplicationTests.java |
Spring boot 入门案例(三)--通过IDEA创建Spirng boot应用
1,新建project/module
2,指定maven坐标以及spirngboot
3,选择Starter
4,创建项目
图片可能来源于网络,如有侵权请告知。
文章中的资料有时忘记书写来源,如果需求请告知
最后:关注一下呗