1、什么是Spring Boot?
Spring Boot是Spring開源組織下的子項目,是Spring組件一站式解決方案,主要是簡化了使用Spring的難度,簡省了繁重的配置,提供了各種啟動器,開發(fā)者能快速上手。
2、Spring Boot有什么好處?
獨立運行
Spring Boot而且內(nèi)嵌了各種servlet容器,Tomcat、Jetty等,現(xiàn)在不再需要打成war包部署到容器中,Spring Boot只要打成一個可執(zhí)行的jar包就能獨立運行,所有的依賴包都在一個jar包內(nèi)。簡化Maven 配置
spring-boot-starter-web啟動器自動依賴其他組件,簡少了maven的配置。自動配置
Spring Boot能根據(jù)當前類路徑下的類、jar包來自動配置bean,如添加一個spring-boot-starter-web啟動器就能擁有web的功能,無需其他配置。無代碼生成和XML配置
Spring Boot配置過程中無代碼生成,也無需XML配置文件就能完成所有配置工作,這一切都是借助于條件注解完成的,這也是Spring4.x的核心功能之一。應(yīng)用監(jiān)控
Spring Boot提供一系列端點可以監(jiān)控服務(wù)及應(yīng)用,做健康檢測。
3、Spring Boot開啟的2種方式
(1) 繼承spring-boot-starter-parent項目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
(2) 導入spring-boot-dependencies項目依賴
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
4、Spring Boot 核心配置
核心注解
(1)@SpringBootApplication
用在 Spring Boot 主類上,標識這是一個 Spring Boot 應(yīng)用,用來開啟 Spring Boot 的各項能力。
其實這個注解就是 @SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan 這三個注解的組合。
(2)@EnableAutoConfiguration
允許 Spring Boot 自動配置注解,開啟這個注解之后,Spring Boot 就能根據(jù)當前類路徑下的包或者類來配置 Spring Bean。
如:當前類路徑下有 Mybatis 這個 JAR 包,MybatisAutoConfiguration 注解就能根據(jù)相關(guān)參數(shù)來配置 Mybatis 的各個 Spring Bean。
(3)@ComponentScan
用來代替配置文件中的 component-scan 配置,開啟組件掃描,即自動掃描包路徑下的 @Component 注解進行注冊 bean 實例到 context 中。
(4)@Configuration
這是 Spring 3.0 添加的一個注解,用來代替 applicationContext.xml 配置文件,所有這個配置文件里面能做到的事情都可以通過這個注解所在類來進行注冊。
(5)@SpringBootConfiguration
這個注解就是 @Configuration 注解的變體,只是用來修飾是 Spring Boot 配置而已,或者可利于 Spring Boot 后續(xù)的擴展。
(6)@Conditional
這是 Spring 4.0 添加的新注解,用來標識一個 Spring Bean 或者 Configuration 配置文件,當滿足指定的條件才開啟配置。
配置文件
Spring Boot有兩種類型的配置文件,application和bootstrap文件。Spring Boot會自動加載classpath目前下的這兩個文件,文件格式為properties或者yml格式。
*.properties文件大家都知道是key=value的形式
*.yml是key: value的形式
application配置文件是應(yīng)用級別的,是當前應(yīng)用的配置文件,主要用于 Spring Boot 項目的自動化配置。
bootstrap配置文件是系統(tǒng)級別的,用來加載外部配置,如配置中心的配置信息,也可以用來定義系統(tǒng)不會變化的屬性。bootstatp文件的加載先于application文件。
5、Spring Boot Starters啟動器
Starters是什么?
Starters可以理解為啟動器,它包含了一系列可以集成到應(yīng)用里面的依賴包,你可以一站式集成Spring及其他技術(shù),而不需要到處找示例代碼和依賴包。如你想使用Spring JPA訪問數(shù)據(jù)庫,只要加入spring-boot-starter-data-jpa啟動器依賴就能使用了。
Starters包含了許多項目中需要用到的依賴,它們能快速持續(xù)的運行,都是一系列得到支持的管理傳遞性依賴。
Starters命名
Spring Boot官方的啟動器都是以spring-boot-starter-命名的,代表了一個特定的應(yīng)用類型。
第三方的啟動器不能以spring-boot開頭命名,它們都被Spring Boot官方保留。一般一個第三方的應(yīng)該這樣命名,像mybatis的mybatis-spring-boot-starter。
Starters分類
(1) Spring Boot應(yīng)用類啟動器
| 啟動器名稱 | 功能描述 |
|---|---|
| spring-boot-starter | 包含自動配置、日志、YAML的支持。 |
| spring-boot-starter-web | 使用Spring MVC構(gòu)建web 工程,包含restful,默認使用Tomcat容器。 |
(2) Spring Boot生產(chǎn)啟動器
| 啟動器名稱 | 功能描述 |
|---|---|
| spring-boot-starter-actuator | 提供生產(chǎn)環(huán)境特性,能監(jiān)控管理應(yīng)用。 |
(3) Spring Boot技術(shù)類啟動器
| 啟動器名稱 | 功能描述 |
|---|---|
| spring-boot-starter-json | 提供對JSON的讀寫支持。 |
| spring-boot-starter-logging | 默認的日志啟動器,默認使用Logback。 |
(4) 其他第三方啟動器
6、Spring Boot Server啟動容器配置
spring-boot-starter-web自動攜帶了tomcat依賴,但也可以替換成jetty和undertow,下面是一個替換jetty的示例。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Use Jetty instead -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
7、運行 Spring Boot 應(yīng)用的 3 種方式
(1) 在 IDE 中運行
Spring Boot 默認采用 jar 包內(nèi)嵌 Tomcat、Jetty 等 Server 的方式,并需要提供一個含有 main 方法的主類。這個時候,直接在 IDE 中運行這個 main 方法就能啟動 Spring Boot 應(yīng)用了。
(2) 打包運行
當你的 Spring Boot 準備提測或者上線,都需要打成 jar 包或者 war 包運行,war 包方式這里不說直接丟到 Server 里面運行即可,這里介紹直接運行 jar 包的方式。
java -jar javastack-0.0.1-SNAPSHOT.jar
(3) 用插件運行
可以在 IDE 或者命令行中使用 Maven 和 Gradle 插件來運行 Spring Boot 應(yīng)用。
mvn spring-boot:run
8、Spring Boot讀取配置文件的幾種方式
讀取application文件
在application.yml或者properties文件中添加:
info.address=USA
info.company=Spring
info.degree=high
@Value注解讀取方式
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class InfoConfig1{
@Value("${info.address}")
private String address;
@Value("${info.company}")
private String company;
@Value("${info.degree}")
private String degree;
public String getAddress(){
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getCompany()
{
return company;
}
public void setCompany(String company)
{
this.company = company;
}
public String getDegree()
{
return degree;
}
public void setDegree(String degree)
{
this.degree = degree;
}
}
@ConfigurationProperties注解讀取方式
@Component
@ConfigurationProperties(prefix = "info")
public class InfoConfig2{
private String address;
private String company;
private String degree;
public String getAddress(){
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getCompany()
{
return company;
}
public void setCompany(String company)
{
this.company = company;
}
public String getDegree()
{
return degree;
}
public void setDegree(String degree)
{
this.degree = degree;
}
}
讀取指定文件
資源目錄下建立config/db-config.properties:
db.username=root
db.password=123456
@PropertySource+@Value注解讀取方式
@Component
@PropertySource(value = {"config/db-config.properties"})
public class DBConfig1{
@Value("${db.username}")
private String username;
@Value("${db.password}")
private String password;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
注意:@PropertySource不支持yml文件讀取。
@PropertySource+@ConfigurationProperties注解讀取方式
@Component
@ConfigurationProperties(prefix = "db")
@PropertySource(value = {"config/db-config.properties"})
public class DBConfig2{
private String username;
private String password;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
Environment讀取方式
以上所有加載出來的配置都可以通過Environment注入獲取到。
@Autowired
private Environment env;
// 獲取參數(shù)
String getProperty(String key);
9、Spring Boot - Profile不同環(huán)境配置
假如有開發(fā)、測試、生產(chǎn)三個不同的環(huán)境,需要定義三個不同環(huán)境下的配置。
基于properties文件類型
你可以另外建立3個環(huán)境下的配置文件:
applcation.properties
application-dev.properties
application-sit.properties
application-prod.properties
然后在applcation.properties文件中指定當前的環(huán)境spring.profiles.active=sit,這時候讀取的就是application-test.properties文件。
基于yml文件類型
只需要一個applcation.yml文件就能搞定,使用---標記為文件。
##########開啟dev環(huán)境##########
spring:
profiles:
active: dev
---
##########dev環(huán)境配置##########
spring:
profiles: dev
server:
port: 8080
---
##########sit環(huán)境配置##########
spring:
profiles: sit
server:
port: 8081
---
##########prod環(huán)境配置##########
spring:
profiles: prod
server:
port: 8082
如何通過maven自動控制環(huán)境配置文件?
(1) 首先在pom.xml中配置多環(huán)境文件
<profiles>
<!-- dev環(huán)境配置 -->
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<!-- 為true的時候就表示當沒有指定其他profile為激活狀態(tài)時,該profile就默認會被激活 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- sit環(huán)境配置 -->
<profile>
<id>sit</id>
<properties>
<env>sit</env>
</properties>
</profile>
<!-- prod環(huán)境配置 -->
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
(2) 然后在springboot的.properties文件中配置spring.profiles.active=@env@。。env為pom.xml文件中的env名字保持一致。
spring:
profiles:
active: @env@
(3) 在maven中配置資源文件
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>profiles/**</exclude>
</excludes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources/profiles/${env}</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
文件目錄截圖

(4) 使用maven打包:mvn clean package -Psit 即可使用sit環(huán)境
9、Spring Boot整合Mybatis
(1) 添加pom依賴
<!-- 引入mybatis模塊 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- mybatis分頁插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
<!-- mysql的JDBC驅(qū)動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- alibaba的druid數(shù)據(jù)庫連接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
(2) application.yml添加相關(guān)配置
# 數(shù)據(jù)庫連接配置
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: root1234
type: com.alibaba.druid.pool.DruidDataSource
# mybatis的相關(guān)配置
mybatis:
# xxxMapper.xml文件的位置
mapper-locations: classpath:mapper/**/*.xml
# 指定POJO掃描包來讓mybatis自動掃描到自定義POJO(如果mapper.xml中已經(jīng)指定了結(jié)果集的類型,可以省略)
type-aliases-package: com.sf.handover.entity
configuration:
# 配置下劃線轉(zhuǎn)駝峰(將數(shù)據(jù)庫的帶下劃線_給去掉然后映射到實體類的屬性上去)
map-underscore-to-camel-case: true
# 是否將sql打印到控制面板(該配置會將sql語句和查詢的結(jié)果都打印到控制臺)
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
(3) 在啟動類中添加對mapper包掃描@MapperScan
@MapperScan("com.xxx.xxx.dao")
或者直接在Mapper類上面添加注解@Mapper
編寫相應(yīng)的service,mapper即可。