springboot自定義starter方法及注解實(shí)例
SpringBoot starter
用了springboot 那么久了居然都還沒自定義過starter,想想都覺得羞愧,所以今天來玩一下。
SpringBoot中的starter是一種非常重要的機(jī)制,能夠拋棄以前繁雜的配置,將其統(tǒng)一集成進(jìn)starter,應(yīng)用者只需要在maven中引入starter依賴,SpringBoot就能自動(dòng)掃描到要加載的信息并啟動(dòng)相應(yīng)的默認(rèn)配置。starter讓我們擺脫了各種依賴庫(kù)的處理,需要配置各種信息的困擾。
SpringBoot會(huì)自動(dòng)通過classpath路徑下的類發(fā)現(xiàn)需要的Bean,并注冊(cè)進(jìn)IOC容器。SpringBoot提供了針對(duì)日常企業(yè)應(yīng)用研發(fā)各種場(chǎng)景的spring-boot-starter依賴模塊。所有這些依賴模塊都遵循著約定成俗的默認(rèn)配置,并允許我們調(diào)整這些配置,即遵循“約定大于配置”的理念。
自定義starter
日常工作中有時(shí)有一些獨(dú)立于業(yè)務(wù)之外的功能或模塊,可能這個(gè)項(xiàng)目在用,另一個(gè)項(xiàng)目也要用,如果每次都重新集成的話就會(huì)很麻煩,這時(shí)我們只要把這些功能或模塊封裝成一個(gè)個(gè)starter的話,在使用的時(shí)候引入進(jìn)去就很方便了。
自定義starter步驟
其實(shí)自定義starter很簡(jiǎn)單,大致需要以下5步:
- 新建兩個(gè)模塊,命名規(guī)范: springboot自帶的starter命名規(guī)范為spring-boot-starter-xxx, 自定義的starter命名規(guī)范為xxx-spring-boot-starter
xxx-spring-boot-autoconfigure:自動(dòng)配置核心代碼
xxx-spring-boot-starter:管理依賴
如果不需要將自動(dòng)配置代碼和依賴項(xiàng)管理分離開來,則可以將它們組合到一個(gè)模塊中。只不過springboot
官方建議將兩個(gè)模塊分開。
- 2. 引入spring-boot-autoconfigure依賴
- 3. 創(chuàng)建自定義的XXXProperties 類: 這個(gè)類的屬性根據(jù)需要是要出現(xiàn)在配置文件中的。
- 4. 創(chuàng)建自定義的XXXAutoConfiguration類:這個(gè)類要配置自動(dòng)配置時(shí)的一些邏輯,同時(shí)也要讓XXXProperties 類生效。
- 5. 創(chuàng)建自定義的spring.factories文件:在resources/META-INF創(chuàng)建一個(gè)spring.factories文件和spring-configuration-metadata.json,spring-configuration-metadata.json文件是用于在填寫配置文件時(shí)的智能提示,可要可不要,有的話提示起來更友好。spring.factories用于導(dǎo)入自動(dòng)配置類,必須要有
實(shí)現(xiàn)
我這里為了方便就只創(chuàng)建一個(gè)模塊了,
- 創(chuàng)建一個(gè)模塊,命名為spring-boot-starter-myStarter,對(duì)應(yīng)pom文件
<groupId>com.example</groupId> <artifactId>spring-boot-starter-myStarter</artifactId> <version>1.0</version> <name>my-starter</name>
- 引入spring-boot-autoconfigure依賴 我這里使用的spring-boot-autoconfigure版本是2.6.2
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.6.2</version> </dependency> </dependencies>
- 創(chuàng)建自定義的XXXProperties 類
@ConfigurationProperties(prefix = "com.arron") public class MyStarterProperties { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
再創(chuàng)建一個(gè)MyStarterConfig用于讀取MyStarterProperties 里的屬性
public class MyStarterConfig { private MyStarterProperties myStarterProperties; private String name; public MyStarterConfig(MyStarterProperties myStarterProperties) { this.myStarterProperties = myStarterProperties; } public String getName() { return myStarterProperties.getName(); } public void setName(String name) { this.name = name; } }
- 創(chuàng)建自定義的XXXAutoConfiguration類
@Configuration // EnableConfigurationProperties value數(shù)組中的配置類起作用 @EnableConfigurationProperties(value = {MyStarterProperties.class}) public class MyStarterAutoConfiguration { @Autowired private MyStarterProperties myStarterProperties; @Bean @ConditionalOnMissingBean(MyStarterConfig.class) public MyStarterConfig myStarterConfig(){ return new MyStarterConfig(myStarterProperties); } }
- 在resources/META-INF創(chuàng)建一個(gè)spring.factories文件
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.myStarter.MyStarterAutoConfiguration
spring-configuration-metadata.json
{ "group": [ { "name": "com.arron", "type": "com.example.myStarter.MyStarterProperties", "sourceType": "com.example.myStarter.MyStarterProperties" } ], "properties": [ { "name": "com.arron.name", "type": "java.lang.String", "description": "my start name", "sourceType": "com.example.myStarter.MyStarterProperties", "defaultValue": "MyStarterProperties name" } ] }
打包測(cè)試
找到如圖maven,點(diǎn)擊install,安裝到本地
然后新建一個(gè)項(xiàng)目導(dǎo)包進(jìn)行測(cè)試,創(chuàng)建項(xiàng)目過程就不介紹了。
- 引入依賴
<dependency> <groupId>com.example</groupId> <artifactId>spring-boot-starter-myStarter</artifactId> <version>1.0</version> </dependency>
- 配置文件添加屬性:
com: arron: name: myname
- 單元測(cè)試:
@RunWith(SpringRunner.class) @SpringBootTest class RabbitmqApplicationTests { @Autowired private MyStarterConfig myStarterConfig; @Test public void testMyStarter(){ String name = myStarterConfig.getName(); System.out.println(name); } }
控制臺(tái)輸出:
myname
至此,一個(gè)簡(jiǎn)單自定義的springboot starter就完成了。
注解解釋
下面這些注解在自定義starter是可能會(huì)用到。
- @Conditional:按照一定的條件進(jìn)行判斷,滿足條件給容器注冊(cè)bean
- @ConditionalOnMissingBean:給定的在bean不存在時(shí),則實(shí)例化當(dāng)前Bean
- @ConditionalOnProperty:配置文件中滿足定義的屬性則創(chuàng)建bean,否則不創(chuàng)建
- @ConditionalOnBean:給定的在bean存在時(shí),則實(shí)例化當(dāng)前Bean
- @ConditionalOnClass: 當(dāng)給定的類名在類路徑上存在,則實(shí)例化當(dāng)前Bean
- @ConditionalOnMissingClass :當(dāng)給定的類名在類路徑上不存在,則實(shí)例化當(dāng)前Bean
以上就是springboot自定義starter方法詳解的詳細(xì)內(nèi)容,更多關(guān)于springboot自定義starter的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于Rest的API解決方案(jersey與swagger集成)
下面小編就為大家?guī)硪黄赗est的API解決方案(jersey與swagger集成)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08Java通過SSM完成水果商城批發(fā)平臺(tái)流程
這是一個(gè)使用了java+SSM開發(fā)的網(wǎng)上水果商城批發(fā)平臺(tái),是一個(gè)實(shí)戰(zhàn)小練習(xí),具有水果商城批發(fā)該有的所有功能,感興趣的朋友快來看看吧2022-06-06Java讀文件修改默認(rèn)換行符的實(shí)現(xiàn)
這篇文章主要介紹了Java讀文件修改默認(rèn)換行符的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12項(xiàng)目打包成jar后包無法讀取src/main/resources下文件的解決
本文主要介紹了項(xiàng)目打包成jar后包無法讀取src/main/resources下文件的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04詳解spring cloud整合Swagger2構(gòu)建RESTful服務(wù)的APIs
這篇文章主要介紹了詳解spring cloud整合Swagger2構(gòu)建RESTful服務(wù)的APIs,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01java 內(nèi)嵌Groovy動(dòng)態(tài)腳本操作
這篇文章主要介紹了java 內(nèi)嵌Groovy動(dòng)態(tài)腳本操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07springboot 實(shí)現(xiàn)bean手動(dòng)注入操作
這篇文章主要介紹了springboot 實(shí)現(xiàn)bean手動(dòng)注入操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01使用Springboot搭建OAuth2.0 Server的方法示例
這篇文章主要介紹了使用Springboot搭建OAuth2.0 Server的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08