1. 前言
Springboot
中的自動配置確實方便惯悠,減少了我們開發(fā)上的復(fù)雜性职车,那么自動配置原理是什么呢洲胖?之前我也寫過了一篇文章進行了分析康震。
Springboot 系列(三)Spring Boot 自動配置。
由于自動配置用到了配置文件的綁定宾濒,如果你還不知道常見的配置文件的用法腿短,可以參考這篇文章。
Springboot 系列(二)Spring Boot 配置文件绘梦。
在這一次橘忱,通過學(xué)習(xí) Springboot
自動配置模式,編寫一個自己的 starter
卸奉,用來加深對自動配置的理解钝诚。
熟悉模式,有助于提升編寫的 starter
的規(guī)范性榄棵,編寫自己的 starter
之前先來學(xué)習(xí) Springboot
官方 starter
以及常見框架的整合 starter
的編寫方式 凝颇,可以領(lǐng)略到其中的奧秘。
2. Springboot 官方模式
選擇一個官方的自動配置進行分析疹鳄,這里就選擇常見的配置端口號配置拧略。
2.1. 引入依賴
使用端口號之前我們需要先引入 web 依賴。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
如果你觀察 starter
多的話瘪弓,也許你發(fā)已經(jīng)發(fā)現(xiàn)了一個模式垫蛆,Springboot
官方的 starter
的名字都是 spring-boot-starter-xxxx
命名的。
查看 spring-boot-starter-web
會發(fā)現(xiàn)腺怯,其實這個依賴只是一個空盒子袱饭,除了依賴其他 pom
之外,沒有一行代碼呛占。
這時虑乖,發(fā)現(xiàn)了另外一個模式:starter
只依賴其他 pom
,不做代碼實現(xiàn)晾虑。
那么 spring-boot-starter-web
到底依賴了哪些內(nèi)容疹味?
觀察這個依賴信息,然后再參照其他的官方 starter
走贪,可以找到幾個固定的引入佛猛,可以被稱之為模式的依賴引入。
- 依賴
spring-boot-starter
坠狡。 - 依賴
spring-boot-autoconfigure
继找。
2.2. 自動配置
引入依賴只有配置端口號,像這樣逃沿。
server.port=8090
IDEA 中可以通過點擊 server.port
找到這個配置綁定的類文件婴渡』盟可以看到配置最終會注入到類ServerProperties
類的 port
屬性上。
那么這個 ServerProperties
到底是哪里使用的呢边臼?繼續(xù)查找哄尔,找到一個和 Servlet
的有關(guān)的調(diào)用。
發(fā)現(xiàn)是被 ServletWebServerFactoryCustomizer
類進行了調(diào)用柠并,這個類里面定義了
private final ServerProperties serverProperties;
用來使用配置的屬性岭接。
繼續(xù)查看這個類的調(diào)用,發(fā)現(xiàn)只有一個類使用這個類臼予,這個類是ServletWebServerFactoryAutoConfiguration
鸣戴。
根據(jù)我們對注解的理解,這個類就是自動配置主要類了粘拾。同時自動配置類都是以 AutoConfiguration
結(jié)尾窄锅。
看這個類的幾個注解的意思。
- 優(yōu)先級別較高缰雇。
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
- 只有在
ServletRequest
類存在和是 Web 應(yīng)用時生效入偷。
@ConditionalOnClass(ServletRequest.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
- 開啟了
ServerProperties
的配置綁定。
@EnableConfigurationProperties(ServerProperties.class)
- 導(dǎo)入了幾個類械哟。
@Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,
ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,
ServletWebServerFactoryConfiguration.EmbeddedJetty.class,
ServletWebServerFactoryConfiguration.EmbeddedUndertow.class })
同時注入配置到 Bean 工廠以供其他地方調(diào)用疏之。
@Bean
public ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer(ServerProperties serverProperties) {
return new ServletWebServerFactoryCustomizer(serverProperties);
}
自動配置僅僅是這些東西嗎?根據(jù)之前文章里的分析戒良,我們知道不止代碼体捏,至少還有一個指定自動配置類的配置文件需要讀取。也就是 spring.factories
文件糯崎。
如果你不知道,可以先看這篇文章河泳。Springboot 系列(三)Spring Boot 自動配置 沃呢。
事實確實如此,可以在 spring.factories
中找到上面跟蹤到的類拆挥。
也就是 ServletWebServerFactoryAutoConfiguration
.
根據(jù)上面的分析薄霜,可以發(fā)現(xiàn) Springboot
官方 starter
的幾個模式。
- 使用
XXXProperties
自動綁定XXX
開頭的配置信息纸兔,如:ServerProperties
惰瓜。 - 把
XXXProperties
定義到要使用的類中,如:ServletWebServerFactoryCustomizer
汉矿。 - 編寫一個
XXXAutoConfiguration
崎坊,開啟XXXProperties
的自動配置,限定生效場景洲拇,創(chuàng)建需要的類到Bean
工廠奈揍。如:ServletWebServerFactoryAutoConfiguration
曲尸。
3. 第三方集成模式
Springboot
官方如果把所有的框架都編寫成 starter
,是不現(xiàn)實的男翰。因此很多第三方框架需要主動集成到 springboot
另患,所以我們選擇一個常用的框架分析它的 starter
實現(xiàn)。因為已經(jīng)看過了 springboot
官方 starter
是如何配置的蛾绎, 第三方框架也是類似昆箕,所以在下面觀察的過程中會直接指出相同點,而不再做對比詳細(xì)對比租冠。
這里選擇 mybatis-spring-boot-starter
進行學(xué)習(xí)分析鹏倘。
3.1 引入依賴
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
這里 mybatis
框架的 starter
依賴符合一定的規(guī)則,即 xxx-spring-boot-starter.
觀察這個 starter
肺稀,發(fā)現(xiàn)它也沒有做任何的代碼實現(xiàn)第股,這一點和 springboot
官方一致。
查看 mybatis-spring-boot-starter
的依賴項话原,有很多夕吻,其中和自動配置有關(guān)的主要是。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-autoconfigure</artifactId>
</dependency>
3.2 自動配置
查看 mybatis-spring-boot-autoconfigure
的內(nèi)容發(fā)現(xiàn)和 springboot
官方的 autoconfigure
結(jié)構(gòu)上是差不多的繁仁。
mybatis
的自動配置也是通過 spring.factories
來指明自動配置涉馅,然后通過 XxxAutoConfiguration
綁定 XxxProperties
來進行自動配置.
在原理上,和上面 springboot
官方的 starter
是相同的黄虱,所以不做過多的介紹了稚矿。
4. 編寫自己的 starter
說了那么多,終于到了實操環(huán)節(jié)捻浦,通過上面的介紹晤揣,我們可以大致得出編寫自己的 starter
步驟。
- 創(chuàng)建名字為
xxx-spring-boot-starter
的啟動器項目朱灿。 - 創(chuàng)建名字為
xxx-spring-boot-autoconfigure
的項目昧识。- 編寫屬性綁定類
xxxProperties
. - 編寫服務(wù)類,引入
xxxProperties
. - 編寫自動配置類
XXXAutoConfiguration
注入配置盗扒。 - 創(chuàng)建
spring.factories
文件跪楞,用于指定要自動配置的類。
- 編寫屬性綁定類
- 啟動器項目為空項目侣灶,用來引入
xxx-spring-boot-autoconfigure
等其他依賴甸祭。 - 項目引入
starter
,配置需要配置的信息褥影。
4.1 創(chuàng)建啟動器項目
由于啟動器不需要代碼實現(xiàn)池户,只需要依賴其他項目,所以直接創(chuàng)建一個空的 maven 項目。但是名字要規(guī)范煞檩。
這里創(chuàng)建的 starter
是 myapp-spring-boot-starter
处嫌。
pom 文件非常簡單,只需要引入接下來要創(chuàng)建的 myapp-spring-boot-autoconfigure
.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.codingme.starter</groupId>
<artifactId>myapp-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 啟動器 -->
<dependencies>
<!-- 引入自動配置項目 -->
<dependency>
<groupId>net.codingme.starter</groupId>
<artifactId>myapp-spring-boot-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
4.2 創(chuàng)建自動配置項目
結(jié)合上面對 starter
的分析斟湃,直接創(chuàng)建一個名字為 myapp-spring-boot-autoconfigure
的項目熏迹。項目中只引入 springboot
父項目以及 spring-boot-starter
。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>net.codingme.starter</groupId>
<artifactId>myapp-spring-boot-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myapp-spring-boot-autoconfigure</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
項目的總體結(jié)構(gòu)看圖凝赛。
在 HelloProperties
中通過注解 @ConfigurationProperties(prefix = "myapp.hello")
讓類中的屬性與 myapp.hello
開頭的配置進行綁定注暗。
/**
* <p>
*
* @Author niujinpeng
* @Date 2019/10/29 23:51
*/
@ConfigurationProperties(prefix = "myapp.hello")
public class HelloProperties {
private String suffix;
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
然后在 HelloService
中的 sayHello
方法使用 HelloProperties
中自動綁定的值。
public class HelloService {
HelloProperties helloProperties;
public String sayHello(String name) {
return "Hello " + name + "墓猎," + helloProperties.getSuffix();
}
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
}
為了讓 HelloService
可以自動注入且能正常使用 HelloProperties
捆昏,所以我們在
HelloServiceAutoConfiguration
類中把 HelloProperties.class
引入,然后把 HelloService
注入到 Bean
毙沾。
/**
* web應(yīng)用才生效
*/
@ConditionalOnWebApplication
/**
* 讓屬性文件生效
*/
@EnableConfigurationProperties(HelloProperties.class)
/***
* 聲明是一個配置類
*/
@Configuration
public class HelloServiceAutoConfiguration {
@Autowired
private HelloProperties helloProperties;
@Bean
public HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
最后在 spring.factories
中只需要指定要自動配置的類即可骗卜。
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
net.codingme.starter.HelloServiceAutoConfiguration
到這里,自動配置項目就完成了左胞】懿郑可以在 myapp-spring-boot-autoconfigure
項目執(zhí)行 mvn install
把自動配置項目打包到本地倉庫,然后使用相同的命令把 myapp-spring-boot-starter
安裝到倉庫烤宙。因為后者依賴于前者項目遍烦,所以這里前者需要先進 mvn install
。
4.3 使用自定義的啟動器
創(chuàng)建一個 springboot
項目myapp-spring-boot-starter-test
躺枕。
引入 web
依賴服猪,引入自己編寫的 myapp-spring-boot-starter
.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入自己的 starter -->
<dependency>
<groupId>net.codingme.starter</groupId>
<artifactId>myapp-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
編寫一個 HelloController
注入自動配置里的 HelloService
用于測試。
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String sayHello(String name) {
return helloService.sayHello(name);
}
}
由于 autoConfigure
項目中定義了 sayHello
方法會輸出“Hello”+傳入的 name + 配置的 hello.suffix
拐云,所以我們在 springboot
配置文件中配置這個屬性罢猪。
myapp.hello.suffix=早上好
運行測試項目,訪問 /hello 路徑傳入一個 name 看看自動配置有沒有生效叉瘩。
從測試結(jié)果可以看到自動配置的早上好已經(jīng)生效了坡脐。到這里自己編寫的 starter
也已經(jīng)完工。
項目已經(jīng)傳到 Github.
https://github.com/niumoo/springboot/tree/master/springboot-starter
<完>
個人網(wǎng)站:https://www.codingme.net
如果你喜歡這篇文章房揭,可以關(guān)注公眾號,一起成長晌端。