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