徒手?jǐn)]一個(gè)Spring Boot中的starter,解密自動(dòng)化配置
starter背景
Spring Boot目前已經(jīng)變成了后端開發(fā)這必備技能之一,其中一個(gè)主要原因是Spring Boot中有個(gè)非常重要的機(jī)制(starter機(jī)制)。
starter能夠拋棄以前繁雜的配置,將其統(tǒng)一集成進(jìn)starter,使用的時(shí)候只需要在maven中引入對(duì)應(yīng)的starter依賴即可,Spring Boot就能自動(dòng)掃描到要加載的信息并啟動(dòng)相應(yīng)的默認(rèn)配置。
starter讓我們擺脫了各種依賴庫(kù)的處理,以及各種配置信息的煩惱。SpringBoot會(huì)自動(dòng)通過classpath路徑下的類發(fā)現(xiàn)需要的Bean,并注冊(cè)進(jìn)IOC容器。Spring Boot提供了針對(duì)日常企業(yè)應(yīng)用研發(fā)各種場(chǎng)景的spring-boot-starter依賴模塊。所有這些依賴模塊都遵循著約定成俗的默認(rèn)配置,并允許我們調(diào)整這些配置,即遵循“約定大于配置”的理念。
我們經(jīng)常會(huì)看到或者使用到各種xxx-starter。比如下面幾種:
Spring Boot starter原理
從總體上來看,無(wú)非就是將Jar包作為項(xiàng)目的依賴引入工程。而現(xiàn)在之所以增加了難度,是因?yàn)槲覀円氲氖荢pring Boot Starter,所以我們需要去了解Spring Boot對(duì)Spring Boot Starter的Jar包是如何加載的?下面我簡(jiǎn)單說一下。
SpringBoot 在啟動(dòng)時(shí)會(huì)去依賴的 starter 包中尋找 /META-INF/spring.factories 文件,然后根據(jù)文件中配置的 Jar 包去掃描項(xiàng)目所依賴的 Jar 包,這類似于 Java 的 SPI 機(jī)制。
細(xì)節(jié)上可以使用@Conditional 系列注解實(shí)現(xiàn)更加精確的配置加載Bean的條件。
JavaSPI 實(shí)際上是“基于接口的編程+策略模式+配置文件”組合實(shí)現(xiàn)的動(dòng)態(tài)加載機(jī)制。
自定義starter的條件
如果想自定義Starter,首選需要實(shí)現(xiàn)自動(dòng)化配置,而要實(shí)現(xiàn)自動(dòng)化配置需要滿足以下兩個(gè)條件:
- 能夠自動(dòng)配置項(xiàng)目所需要的配置信息,也就是自動(dòng)加載依賴環(huán)境;
- 能夠根據(jù)項(xiàng)目提供的信息自動(dòng)生成Bean,并且注冊(cè)到Bean管理容器中;
實(shí)現(xiàn)自定義starter
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-autoconfigure</artifactId>
- <version>2.0.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <version>2.0.0.RELEASE</version>
- <optional>true</optional>
- </dependency>
- </dependencies>
根據(jù)需要自定義Starter的實(shí)現(xiàn)過程大致如下(以我定義的Starter為例):
定義XxxProperties類,屬性配置類,完成屬性配置相關(guān)的操作,比如設(shè)置屬性前綴,用于在application.properties中配置。
TianProperties代碼:
- import org.springframework.boot.context.properties.ConfigurationProperties;
- @ConfigurationProperties(prefix = "spring.tian")
- public class TianProperties {
- private String name;
- private int age;
- private String sex = "M";
- //省略 get set 方法
- }
創(chuàng)建XxxService類,完成相關(guān)的操作邏輯 。
TianService代碼:
- public class TianService {
- private TianProperties properties;
- public TianService() {
- }
- public TianService(TianProperties userProperties) {
- this.properties = userProperties;
- }
- public void sayHello(){
- System.out.println("hi, 我叫: " + properties.getName() +
- ", 今年" + properties.getAge() + "歲"
- + ", 性別: " + properties.getSex());
- }
- }
定義XxxConfigurationProperties類,自動(dòng)配置類,用于完成Bean創(chuàng)建等工作。
TianServiceAutoConfiguration代碼:
- @Configuration
- @EnableConfigurationProperties(TianProperties.class)
- @ConditionalOnClass(TianService.class)
- @ConditionalOnProperty(prefix = "spring.tian", value = "enabled", matchIfMissing = true)
- public class TianServiceAutoConfiguration {
- @Autowired
- private TianProperties properties;
- @Bean
- @ConditionalOnMissingBean(TianService.class)
- public TianService tianService() {
- return new TianService(properties);
- }
- }
在resources下創(chuàng)建目錄META-INF,在 META-INF 目錄下創(chuàng)建 spring.factories,在SpringBoot啟動(dòng)時(shí)會(huì)根據(jù)此文件來加載項(xiàng)目的自動(dòng)化配置類。
「spring.factories中配置」
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.tian.TianServiceAutoConfiguration
把上面這個(gè)starter工程打成jar包:
使用自定義starter
創(chuàng)建一個(gè)Spring Boot項(xiàng)目test,項(xiàng)目整體如下圖:
在項(xiàng)目中把自定義starter添加pom依賴
- <dependency>
- <groupId>com.tian</groupId>
- <artifactId>spring-boot-tian-starter</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
TestApplication啟動(dòng)類
- @SpringBootApplication
- @EnableEurekaServer
- public class TestApplication {
- public static void main(String[] args) {
- SpringApplication.run(TestApplication.class, args);
- }
- }
application.properties中配置
- spring.tian.name=tian
- spring.tian.age=22
- spring.tian.sex=M
寫一個(gè)TestController.java類
- RestController
- @RequestMapping("/my")
- public class TestController {
- @Resource
- private TianService tianService;
- @PostMapping("/starter")
- public Object starter() {
- tianService.sayHello();
- return "ok";
- }
- }
把我們自定義的starter打成的jar依賴進(jìn)來后,
可以看到其中多了一個(gè)json的文件。
最后啟動(dòng)項(xiàng)目,輸入
http://localhost:9091/my/starter
controller成功返回ok,再看后臺(tái)打印
- hi, 我叫: tian, 今年22歲, 性別: M
這就成功的現(xiàn)實(shí)了自定義的starter。
關(guān)鍵詞:開箱即用、減少大量的配置項(xiàng)、約定大于配置。
總結(jié)
- Spring Boot在啟動(dòng)時(shí)掃描項(xiàng)目所依賴的JAR包,尋找包含spring.factories文件的JAR包,
- 然后讀取spring.factories文件獲取配置的自動(dòng)配置類AutoConfiguration`,
- 然后將自動(dòng)配置類下滿足條件(@ConditionalOnXxx)的@Bean放入到Spring容器中(Spring Context)
- 這樣使用者就可以直接用來注入,因?yàn)樵擃愐呀?jīng)在容器中了。
本文轉(zhuǎn)載自微信公眾號(hào)「Java后端技術(shù)全棧」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系Java后端技術(shù)全棧公眾號(hào)。