教你利用SpringBoot寫(xiě)一個(gè)屬于自己的Starter
(一)概述
SpringBoot以其自動(dòng)裝配的能力被廣泛應(yīng)用,我們?cè)趯?xiě)代碼時(shí)肯定遇到過(guò)很多spring-boot-starter命名的依賴(lài),比如spring-boot-starter-web,在pom文件中引入這些starter依賴(lài)后,SpringBoot就能通過(guò)自動(dòng)裝配的技術(shù)掃描到這些類(lèi)并裝載到Bean容器中。
除了SpringBoot官方的這些Starter外,我們自己也可以開(kāi)發(fā)Starter。為了和官方的starter區(qū)分,建議自定義的starter命名格式為xxxx-spring-boot-starter,比如mybatis-spring-boot-starter。
本文將介紹如何自己實(shí)現(xiàn)一個(gè)starter。
(二)看個(gè)例子
在寫(xiě)自己的Starter之前,很有必要看一下別人是怎樣去寫(xiě)的。這里拿RedisAutoConfiguration這個(gè)類(lèi)作為例子。
SpringBoot在啟動(dòng)時(shí)會(huì)通過(guò)自動(dòng)裝配去掃描項(xiàng)目MATA-INF/spring.factories文件,這個(gè)文件中定義了所有需要去自動(dòng)裝配的類(lèi)。Redis的自動(dòng)裝配類(lèi)就是下圖的RedisAutoConfiguration。
進(jìn)入 RedisAutoConfiguration后,首先看最重要的幾個(gè)注解,@Configuration不用多提了,@ConditionalOnClass表示當(dāng)這個(gè)注解后面的類(lèi)存在時(shí),該Bean才會(huì)被加載。下圖中很明顯RedisOperations并不存在,所以Redis不會(huì)被自動(dòng)裝配進(jìn)去。
@EnableConfigurationProperties用于自動(dòng)加載配置類(lèi)的信息,配置類(lèi)和我們平常寫(xiě)的基本一樣,通過(guò)ConfigurationProperties讀取properties或者yaml中的配置信息。
RedisAutoConfiguration這個(gè)類(lèi)中定義的Bean有兩個(gè),我們應(yīng)該都比較熟悉。@ConditionalOnMissingBean的意思是當(dāng)Spring容器中不存在這個(gè)Bean的時(shí)候,才會(huì)加載這個(gè)Bean,所以如果我們?cè)诖a中自己定義了redisTemplate之后,注入到Bean容器中的就是我們自己寫(xiě)的那個(gè)Bean。
看到這里基本已經(jīng)知道一個(gè)Starter的實(shí)現(xiàn)方案了,接下來(lái)就寫(xiě)一個(gè)簡(jiǎn)單的Starter。
(三)實(shí)現(xiàn)信息播報(bào)Starter
要做的這個(gè)Starter其實(shí)很簡(jiǎn)單,就是輸出配置文件中配置的信息。
首先新建一個(gè)Maven項(xiàng)目,在項(xiàng)目中新建兩個(gè)Module,我給兩個(gè)項(xiàng)目分別命名為report-spring-boot-starter和example-spring-boot
首先介紹report-spring-boot-starter,這是一個(gè)Starter,主要實(shí)現(xiàn)輸出一些內(nèi)容的功能,我們可以完全按照RedisAutoConfiguration去寫(xiě)。首先新建自動(dòng)裝配類(lèi):ReportAutoConfiguration
@Configuration @ConditionalOnClass(ReportOperation.class) @EnableConfigurationProperties(ReportProperties.class) public class ReportAutoConfiguration { @Bean public ReportOperation reportOperation(ReportProperties reportProperties){ ReportOperation reportOperation = new ReportOperation(reportProperties.getMsg()); return reportOperation; } }
當(dāng)ReportOperation存在時(shí)才會(huì)加載該配置,激活配置文件ReportProperties
public class ReportOperation { private String msg; public ReportOperation(String msg){ this.msg = msg; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String report(){ return msg; } }
ReportProperties如下:
@ConfigurationProperties(prefix = "report") public class ReportProperties { private String msg; public void setMsg(String msg) { this.msg = msg; } public String getMsg() { return msg; } }
接下來(lái)是實(shí)現(xiàn)自動(dòng)注入的關(guān)鍵,前面已經(jīng)說(shuō)到了,SpringBoot會(huì)去掃描依賴(lài)Jar包中META-INF/spring.factories中的內(nèi)容,因此在resources目錄下新建META-INF/spring.factories,寫(xiě)下配置信息:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.javayz.starter.ReportAutoConfiguration
這樣一個(gè)簡(jiǎn)單的Starter就完成了。
(四)調(diào)用這個(gè)Starter
接下來(lái)在example-spring-boot這個(gè)module中調(diào)用上面的starter,首先第一步引入相關(guān)依賴(lài),這里只需要引入springboot相關(guān)依賴(lài)和自己寫(xiě)的starter依賴(lài)即可:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.example</groupId> <artifactId>report-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency>
接著寫(xiě)一個(gè)測(cè)試的controller測(cè)試自動(dòng)注入的效果:
@RestController public class TestController { @Autowired private ReportOperation reportOperation; @GetMapping("test") public String test(){ System.out.println(reportOperation.getMsg()); return reportOperation.getMsg(); } }
在配置文件中增加一條配置:
report.msg = hello
運(yùn)行項(xiàng)目調(diào)用接口,可以發(fā)現(xiàn)ReportOperation這個(gè)Bean已經(jīng)被自動(dòng)注入了。
(五)總結(jié)
本文結(jié)合redis自動(dòng)注入的例子,寫(xiě)了一個(gè)屬于自己的Starter,希望對(duì)大家有所幫助。我是魚(yú)仔,我們下期再見(jiàn)!
到此這篇關(guān)于利用SpringBoot寫(xiě)一個(gè)屬于自己的Starter的文章就介紹到這了,更多相關(guān)SpringBoot寫(xiě)一個(gè)Starter內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java web用servlet監(jiān)聽(tīng)器實(shí)現(xiàn)顯示在線(xiàn)人數(shù)
這篇文章主要為大家詳細(xì)介紹了java web用servlet監(jiān)聽(tīng)器實(shí)現(xiàn)顯示在線(xiàn)人數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03淺談java Properties類(lèi)的使用基礎(chǔ)
下面小編就為大家分享一篇淺談java Properties類(lèi)的使用基礎(chǔ),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01SpringBoot2.0解決Long型數(shù)據(jù)轉(zhuǎn)換成json格式時(shí)丟失精度問(wèn)題
這篇文章主要介紹了SpringBoot2.0解決Long型數(shù)據(jù)轉(zhuǎn)換成json格式時(shí)丟失精度問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Spring框架開(kāi)發(fā)scope作用域分析總結(jié)
這篇文章主要介紹了Spring框架開(kāi)發(fā)中scope作用域的分析總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-09-09Spring?Boot?中的?@HystrixCommand?注解原理及使用方法
通過(guò)使用 @HystrixCommand 注解,我們可以輕松地實(shí)現(xiàn)對(duì)方法的隔離和監(jiān)控,從而提高系統(tǒng)的可靠性和穩(wěn)定性,本文介紹了Spring Boot 中的@HystrixCommand注解是什么,其原理以及如何使用,感興趣的朋友跟隨小編一起看看吧2023-07-07Java通俗易懂系列設(shè)計(jì)模式之責(zé)任鏈模式
這篇文章主要介紹了Java通俗易懂系列設(shè)計(jì)模式之責(zé)任鏈模式,對(duì)設(shè)計(jì)模式感興趣的同學(xué),一定要看一下2021-04-04Spring中@Autowired和@Resource注解的使用區(qū)別詳解
這篇文章主要介紹了Spring中@Autowired和@Resource注解的使用區(qū)別詳解,@Autowired默認(rèn)根據(jù)type進(jìn)行注入,找到與指定類(lèi)型兼容的?Bean?并進(jìn)行注入,如果無(wú)法通過(guò)type匹配到對(duì)應(yīng)的?Bean?的話(huà),會(huì)根據(jù)name進(jìn)行匹配,如果都匹配不到則拋出異常,需要的朋友可以參考下2023-11-11