學(xué)習(xí)網(wǎng)站:spring boot
Spring Boot的優(yōu)勢(shì)
自動(dòng)配置
根據(jù)Classpath中的包自動(dòng)配置相應(yīng)的bean.
起步依賴
作用
-
使用場(chǎng)景:
一般我們開(kāi)發(fā)一個(gè)web程序,如果想使用Thymeleaf視圖、通過(guò)JPA進(jìn)行數(shù)據(jù)持久化。
為了添加Thymeleaf視圖和Jpa的支持,我們不得不添加相關(guān)依賴,而添加依賴我們不僅需要知道每個(gè)依賴的坐標(biāo)而且還要保證各依賴之間的兼容。光一個(gè)一個(gè)找這些依賴就比較麻煩了。而且在還沒(méi)有代碼的情況,我們是不知道添加的依賴到底完不完整的。所以開(kāi)發(fā)一個(gè)web程序還沒(méi)寫(xiě)一行代碼之前,光是找各種依賴架包就是一個(gè)挺麻煩的事情了。而且,如果我們下次再寫(xiě)一個(gè)web程序時(shí),好的情況是直接把上次找好的依賴添加過(guò)來(lái),壞的情況是,新寫(xiě)的web需要添加新的依賴,而這個(gè)依賴很有可能導(dǎo)致和其它依賴版本不兼容,所以還會(huì)產(chǎn)生重新找的問(wèn)題。 -
起步依賴作用
如果我們?cè)跇?gòu)建文件里指定這些功能:開(kāi)發(fā)Web程序,使用Thymeleaf視圖和Jpa的支持。讓構(gòu)建過(guò)程自己搞明白我們需要什么東西,豈不是更簡(jiǎn)單?這正是Spring Boot起步依賴的功能。
指定基于功能的依賴
- Spirng Boot通過(guò)提供眾多起步依賴降低項(xiàng)目依賴的復(fù)雜度。起步依賴本質(zhì)上是一個(gè)Maven項(xiàng)目對(duì)象模型(Project Object Model,POM),定義了對(duì)其他庫(kù)的傳遞依賴,這些東西加在一起即支持某項(xiàng)功能。 很多起步依賴度額命名都暗示了它們提供的某種或某類功能。
比如起步依賴spring-boot-starter-web,他是一個(gè)package類型為jar的maven項(xiàng)目,項(xiàng)目里除了一個(gè)pom.xml文件外沒(méi)有任何代碼文件。而pom.xml里則聲明了對(duì)各種web開(kāi)發(fā)可能用到依賴。 - spring-boot-starter-web起步依賴的pom.xml中依賴的聲明:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
</dependencies>
- 由此可見(jiàn),起步依賴是服務(wù)于某一類功能的一些依賴的集合,當(dāng)在項(xiàng)目中聲明起步依賴時(shí),通過(guò)依賴傳遞,就相當(dāng)于聲明了其它依賴。再簡(jiǎn)單點(diǎn):spring把大部分依賴按照功能進(jìn)行分類,然后通過(guò)起步依賴來(lái)管理每一類。每一個(gè)起步依賴都代表一類依賴。
- 舉例
- 你打算做一個(gè)閱讀列表Web應(yīng)用程序。與其向項(xiàng)目的構(gòu)建文件中添加一堆單獨(dú)庫(kù)依賴,還不如聲明這是一個(gè)Web應(yīng)用程序來(lái)的簡(jiǎn)單。你只要添加Spring Boot的Web起步依賴就好了。
- 想以Thymeleaf為Web視圖,用JPA來(lái)實(shí)現(xiàn)數(shù)據(jù)持久化,因此在構(gòu)建文件中添加Thymeleaf和Spring Data JPA的起步依賴。為了進(jìn)行測(cè)試,還需要能在Spring Boot上下文里運(yùn)行集成測(cè)試的庫(kù),因此添加test起步依賴。
最終添加的依賴如下:
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
...
這4個(gè)起步依賴的具體程度恰到好處,我們并沒(méi)有說(shuō)想要SpringMVC,只是想說(shuō)要構(gòu)建一個(gè)Web應(yīng)用程序。并沒(méi)有說(shuō)指定JUnit或其他測(cè)試工具,只是所想要測(cè)試自己的代碼。Thymeleaf和Spring Data JPA的起步依賴稍微具體一點(diǎn)。
如果你想知道自己聲明了那些依賴,通過(guò)maven構(gòu)建的項(xiàng)目,可以使用mvn dependency:tree命令來(lái)查看依賴樹(shù)。
起步依賴沒(méi)有指定版本號(hào),因?yàn)槠鸩揭蕾嚤旧硎怯烧谑褂玫膕pring-boot-starter-parent的版本來(lái)決定的,而起步依賴的版本則決定它們引入的傳遞依賴的版本。
當(dāng)前spring-boot項(xiàng)目-->spring-boot-starter-parent-->spring-boot-dependencies。而在spring-boot-dependencies中的依賴管理中申明了所有起步依賴。所以說(shuō)起步依賴本身的版本有正在使用的spring-boot-starter-parent的版本決定。
spring boot官方起步依賴的命名規(guī)則:spring-boot-starter-*。 *代表具體的模塊。比如spring-boot-starter-web。這就是web開(kāi)發(fā)的起步依賴。
spring官網(wǎng)的一些摘要
Spring Boot provides a number of “Starters” that make easy to add jars to your classpath.
其中spring-boot-starter-parent起步依賴,spring-boot一般都會(huì)繼承它,它不會(huì)往項(xiàng)目中添加任何依賴,它只是提供了一個(gè)依賴管理(dependency-management ),這樣你就可以不需要為后續(xù)的依賴申明版本了。
Maven users can inherit from the spring-boot-starter-parent project to obtain sensible defaults. The parent project provides the following features:
Java 1.6 as the default compiler level.
UTF-8 source encoding.
A Dependency Management section, allowing you to omit <version> tags for common dependencies, inherited from the spring-boot-dependencies POM.
Sensible resource filtering.
Sensible plugin configuration (exec plugin, surefire, Git commit ID, shade).
Sensible resource filtering for application.properties and application.yml including profile-specific files (e.g. application-foo.properties and application-foo.yml)
spring-boot-starter-parent繼承了spring-boot-dependencies
覆蓋起步依賴引入的傳遞依賴
大部分情況下,你都無(wú)需關(guān)心每個(gè)spring boot起步依賴分別聲明了些什么東西。Web起步依賴讓你構(gòu)建Web應(yīng)用程序,Thymeleaf起步依賴讓你用Thymeleaf模板。但是即使經(jīng)過(guò)Spring Boot團(tuán)隊(duì)的測(cè)試,起步依賴?yán)锼x的依賴仍有問(wèn)題怎么辦,如何覆蓋起步依賴呢?
覆蓋起步依賴中通過(guò)傳遞依賴加入的依賴有兩種原因:1.起步依賴中聲明的依賴我用不到,為了給程序瘦身,在打包的時(shí)候我想把沒(méi)用的依賴都排除掉,比如web起步依賴中聲明了jackson依賴,但是這個(gè)依賴我在程序中用不到。2.起步依賴中聲明的依賴版本不滿足要求。
針對(duì)第1種情況,如果使用maven構(gòu)建項(xiàng)目,則使用exclusions標(biāo)簽來(lái)排除。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
</exclusions>
</dependency>
- 針對(duì)第2中情況:有兩種辦法
1:因?yàn)樗衅鸩揭蕾嚿婕暗囊蕾嚨陌姹径荚?spring-boot-dependencies
中聲明了,所以可以在當(dāng)前項(xiàng)目的pom.xml中覆蓋之前屬性。比如
<properties>
<spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>
在做這個(gè)之前你需要知道你要修改的依賴的版本屬性在spring-boot-dependencies中的名稱。spring-boot-dependencies ->pom 。遺憾的是,不是每一個(gè)依賴的版本在dependencies 都有聲明,像web起步依賴中的jackson依賴在dependencies 中就沒(méi)有關(guān)于依賴版本屬性的聲明,那么它的版本在哪里呢?答:該依賴是dependencies 中聲明的某個(gè)依賴傳遞過(guò)來(lái)的。面對(duì)這種情況只能采用第二種辦法。
2:根據(jù)maven的就近原則。在當(dāng)前項(xiàng)目中聲明該依賴,則傳遞過(guò)來(lái)的依賴就會(huì)被取代。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>填寫(xiě)你想要的版本</veriosn>
</dependency>
命令行界面
Actuator
Spring Boot程序推薦的代碼組織結(jié)構(gòu)
構(gòu)造函數(shù)依賴注入
package com.example.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DatabaseAccountService implements AccountService {
private final RiskAssessor riskAssessor;
@Autowired
public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
}
// ...
}
Notice how using constructor injection allows the riskAssessor field to be marked as final, indicating that it cannot be subsequently changed.