SpringBoot定制化Starter實現(xiàn)方法
Spring Boot Starter官網(wǎng)描述:Spring Boot Starter官方介紹
什么是Spring Boot Starter
Starters可以理解為啟動器,它包含了一系列可以集成到應用里面的依賴包,可以一站式集成 Spring和其他技術,而不需要到處找示例代碼和依賴包。Spring Boot Starter的工作原理是:Spring Boot在啟動時掃描項目所依賴的JAR包,尋找包含spring.factories文件的JAR包,根據(jù)spring.factories配置加載AutoConfigure類,根據(jù)@Conditional注解的條件,進行自動配置并將Bean注入Spring Context
為什么要自定義Spring Boot Starter?
在Spring Boot官網(wǎng)為了簡化我們的開發(fā),已經(jīng)提供了非常多場景的Starter來為我們使用,即便如此,也無法全面的滿足我們實際工作中的開發(fā)場景,這時我們就需要自定義實現(xiàn)定制化的Starter。

實現(xiàn)步驟
1.首先,創(chuàng)建一個Maven空工程,添加兩個模塊
啟動器
啟動器中沒有任何的源代碼,只是告訴我們當前場景需要引入哪些依賴即可!
創(chuàng)建啟動器模塊為maven工程,命名為
xiaozhao-hello-spring-boot-starter,對應的依賴文件
<groupId>com.zhao</groupId> <artifactId>xiaozhao-hello-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version>
自動配置包
自動配置包中實現(xiàn)了所有的自動配置功能!
創(chuàng)建自動配置包模塊為SpringBoot初始化工程,命名為xiaozhao-hello-spring-boot-starter-autoconfigure

最終的項目模塊如下:

2.模塊創(chuàng)建完成后,需要在啟動器中引入自動配置模塊(別人引入場景啟動器,自動配置包就會自動引入)
<dependencies>
<dependency>
<groupId>com.zhao</groupId>
<artifactId>xiaozhao-hello-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
3.編寫自動配置模塊。
創(chuàng)建自定義的Properties文件
@ConfigurationProperties("xiaozhao.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
創(chuàng)建業(yè)務類讀取Properties文件中的值
public class HelloService {
@Autowired
HelloProperties helloProperties;
public String sayHello(String userName){
return helloProperties.getPrefix() + ":" + userName + ">" + helloProperties.getSuffix();
}
}
再個自動配置類,自動進行類加載
@Configuration
@ConditionalOnMissingBean(HelloService.class)
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
return helloService;
}
}
最終的效果如下:

4.在resources目錄下創(chuàng)建META-INF/spring.factories添加如下配置信息
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zhao.hello.auto.HelloServiceAutoConfiguration

5.裝hello-spring-boot-starter-autoconfigure模塊和xiaozhao-hello-spring-boot-starter


6.裝完成后,創(chuàng)建新的項目來引入創(chuàng)建好的starter.新的的項目名為hello-test的Spring Boot初始化項目。

引入我們之前定義的Starter啟動器和Spring Boot自帶的Web啟動器
<dependencies>
<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>
<dependency>
<groupId>com.zhao</groupId>
<artifactId>xiaozhao-hello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

7.在hello-test項目中創(chuàng)建測試Controller
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String sayHello() {
String str = helloService.sayHello("李四");
return str;
}
}
編寫配置文件
xiaozhao.hello.prefix=hello
xiaozhao.hello.suffix=666

8.啟動項目,測試一下

總結(jié)自定義Starter的實現(xiàn)邏輯
- 首先引入自定義的Starter:xiaozhao-hello-spring-boot-starter,在這個Starter中引入自定義的自動配置場景
- 當自動配置場景啟動時,會去尋找spring.factories文件,去自動加載HelloServiceAutoConfiguration類文件
- 加載完自動配置類后,@ConditionalOnMissingBean(HelloService.class)通過這個注解,當容器中沒有HelloService時,去自動添加一個HelloService組件。
- HelloService組件的所有屬性,通過HelloProperties配置文件進行綁定的,@ConfigurationProperties("xiaozhao.hello"),通過xiaozhao.hello。xxx進行綁定。
- 如果自己在容器中注入一個HelloService組件,使用的就不是自動配置的,而是重新注入的。
到此這篇關于SpringBoot定制化Starter實現(xiàn)方法的文章就介紹到這了,更多相關SpringBoot Starter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
IDEA創(chuàng)建Java?Web項目的超詳細圖文教學
IDEA是程序員們常用的java集成開發(fā)環(huán)境,也是被公認為最好用的java開發(fā)工具,下面這篇文章主要給大家介紹了關于IDEA創(chuàng)建Java?Web項目的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2022-12-12
解決idea使用過程中讓你覺得不爽的一些問題(小結(jié))
這篇文章主要介紹了解決idea使用過程中讓你覺得不爽的一些問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
詳解springboot + profile(不同環(huán)境讀取不同配置)
本篇文章主要介紹了springboot + profile(不同環(huán)境讀取不同配置),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
java多線程實現(xiàn)同步鎖賣票實戰(zhàn)項目
本文主要介紹了java多線程實現(xiàn)同步鎖賣票實戰(zhàn)項目,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01

