溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring Boot中的starter原理以及如何進(jìn)行自動(dòng)化配置

發(fā)布時(shí)間:2021-10-27 15:42:19 來源:億速云 閱讀:357 作者:柒染 欄目:安全技術(shù)

本篇文章給大家分享的是有關(guān)Spring Boot中的starter原理以及如何進(jìn)行自動(dòng)化配置,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

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原理以及如何進(jìn)行自動(dòng)化配置

Spring Boot starter原理

從總體上來看,無非就是將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è)條件:

  1. 鴻蒙官方戰(zhàn)略合作共建——HarmonyOS技術(shù)社區(qū)

  2. 能夠自動(dòng)配置項(xiàng)目所需要的配置信息,也就是自動(dòng)加載依賴環(huán)境;

  3. 能夠根據(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為例):

Spring Boot中的starter原理以及如何進(jìn)行自動(dòng)化配置

定義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包:

Spring Boot中的starter原理以及如何進(jìn)行自動(dòng)化配置

使用自定義starter

創(chuàng)建一個(gè)Spring Boot項(xiàng)目test,項(xiàng)目整體如下圖:

Spring Boot中的starter原理以及如何進(jìn)行自動(dò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)來后,

Spring Boot中的starter原理以及如何進(jìn)行自動(dòng)化配置

可以看到其中多了一個(gè)json的文件。

Spring Boot中的starter原理以及如何進(jìn)行自動(dòng)化配置

最后啟動(dòng)項(xiàng)目,輸入

http://localhost:9091/my/starter

Spring Boot中的starter原理以及如何進(jìn)行自動(dòng)化配置

controller成功返回ok,再看后臺(tái)打印

hi, 我叫: tian, 今年22歲, 性別: M

這就成功的現(xiàn)實(shí)了自定義的starter。

關(guān)鍵詞:開箱即用、減少大量的配置項(xiàng)、約定大于配置。

總結(jié)

  1. Spring Boot在啟動(dòng)時(shí)掃描項(xiàng)目所依賴的JAR包,尋找包含spring.factories文件的JAR包,

  2. 然后讀取spring.factories文件獲取配置的自動(dòng)配置類AutoConfiguration`,

  3. 然后將自動(dòng)配置類下滿足條件(@ConditionalOnXxx)的@Bean放入到Spring容器中(Spring Context)

  4. 這樣使用者就可以直接用來注入,因?yàn)樵擃愐呀?jīng)在容器中了。

以上就是Spring Boot中的starter原理以及如何進(jìn)行自動(dòng)化配置,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI