如何自定義一個(gè)SpringBoot?Starter(步驟和代碼示例)
創(chuàng)建自定義 Spring Boot Starter 涉及封裝特定功能供其他項(xiàng)目復(fù)用。以下是詳細(xì)步驟和代碼示例:
核心步驟
?項(xiàng)目結(jié)構(gòu)?
創(chuàng)建 Maven 項(xiàng)目(兩個(gè)模塊):
my-starter-parent ├── my-spring-boot-autoconfigure // 自動(dòng)配置核心 └── my-spring-boot-starter // 輕量依賴聚合
1. 創(chuàng)建自動(dòng)配置模塊 (my-spring-boot-autoconfigure)
① POM 依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>3.1.0</version> <optional>true</optional> </dependency> </dependencies>
② 創(chuàng)建配置屬性類
@ConfigurationProperties(prefix = "my.service") public class MyServiceProperties { private String prefix = "Hello"; private String suffix = "!"; // 省略 getter/setter }
③ 編寫業(yè)務(wù)服務(wù)類
public class MyService { private final String prefix; private final String suffix; public MyService(String prefix, String suffix) { this.prefix = prefix; this.suffix = suffix; } public String greet(String name) { return prefix + " " + name + suffix; } }
④ 自動(dòng)配置類(核心)
@Configuration @EnableConfigurationProperties(MyServiceProperties.class) @ConditionalOnClass(MyService.class) // 類路徑存在時(shí)生效 @ConditionalOnProperty(prefix = "my.service", name = "enabled", matchIfMissing = true) public class MyServiceAutoConfiguration { @Bean @ConditionalOnMissingBean // 用戶未定義時(shí)初始化 public MyService myService(MyServiceProperties properties) { return new MyService(properties.getPrefix(), properties.getSuffix()); } }
⑤ 注冊(cè)自動(dòng)配置
在 src/main/resources/META-INF/spring/
創(chuàng)建文件:
?文件路徑: org.springframework.boot.autoconfigure.AutoConfiguration.imports
com.example.autoconfigure.MyServiceAutoConfiguration
2. 創(chuàng)建 Starter 模塊 (my-spring-boot-starter)
POM 配置
<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>my-spring-boot-autoconfigure</artifactId> <version>1.0.0</version> </dependency> </dependencies>
?? ?關(guān)鍵點(diǎn)?:Starter 本身無代碼,僅聚合依賴
3. 安裝到本地倉庫
mvn clean install
4. 在其他項(xiàng)目中使用
添加依賴
<dependency> <groupId>com.example</groupId> <artifactId>my-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency>
配置參數(shù) (application.yml)
my: service: prefix: "Welcome" suffix: "!!!" # enabled: true # 默認(rèn)啟用
代碼調(diào)用
@RestController public class DemoController { @Autowired private MyService myService; @GetMapping("/greet") public String greet(String name) { return myService.greet(name); } }
高級(jí)配置技巧
?條件化Bean?
使用 Spring Boot 的條件注解控制Bean創(chuàng)建:
@Bean @ConditionalOnWebApplication // 僅Web環(huán)境生效 @ConditionalOnMissingBean public MyWebService myWebService() {...}
?自定義指標(biāo)監(jiān)控?
集成 Micrometer 暴露指標(biāo):
@Bean public MyServiceMetrics myServiceMetrics(MyService service) { return new MyServiceMetrics(service); }
?Starter 元數(shù)據(jù)?
在 autoconfigure
模塊的 META-INF/spring-configuration-metadata.json
中添加配置提示:
{ "properties": [ { "name": "my.service.prefix", "type": "java.lang.String", "defaultValue": "Hello", "description": "Custom greeting prefix." } ] }
調(diào)試技巧
啟用 debug 模式查看自動(dòng)配置報(bào)告:
debug: true
檢查 Condition Evaluation Report 中的 Positive matches
和 Negative matches
通過以上步驟,您已創(chuàng)建了一個(gè)可復(fù)用、可配置的 Spring Boot Starter。關(guān)鍵點(diǎn)在于解耦自動(dòng)配置與依賴管理,并遵循 Spring Boot 的約定大于配置原則。實(shí)際開發(fā)中可結(jié)合具體需求擴(kuò)展錯(cuò)誤處理、健康檢查等企業(yè)級(jí)特性。
到此這篇關(guān)于如何自定義一個(gè)SpringBoot Starter(步驟和代碼示例)的文章就介紹到這了,更多相關(guān)自定義SpringBoot Starter內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring實(shí)現(xiàn)數(shù)據(jù)庫讀寫分離詳解
這篇文章主要介紹了Spring?實(shí)現(xiàn)數(shù)據(jù)庫讀寫分離,大多數(shù)系統(tǒng)都是讀多寫少,為了降低數(shù)據(jù)庫的壓力,可以對(duì)主庫創(chuàng)建多個(gè)從庫,從庫自動(dòng)從主庫同步數(shù)據(jù),程序中將寫的操作發(fā)送到主庫,將讀的操作發(fā)送到從庫去執(zhí)行,需要的朋友可以參考下2024-01-01深入研究spring boot集成kafka之spring-kafka底層原理
這篇文章主要深入研究了spring boot集成kafka如何實(shí)現(xiàn)spring-kafka的底層原理分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02如何在SpringBoot+Freemarker中獲取項(xiàng)目根目錄
這篇文章主要介紹了如何在SpringBoot+Freemarker中獲取項(xiàng)目根目錄的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10JavaWeb使用mvc模式實(shí)現(xiàn)登錄功能
本文主要介紹了JavaWeb使用mvc模式實(shí)現(xiàn)登錄功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Java中bcrypt算法實(shí)現(xiàn)密碼加密的方法步驟
我們可以在Spring Boot和SSM中實(shí)現(xiàn)密碼加密,使用bcrypt算法可以保障密碼的安全性,并且減少了手動(dòng)編寫哈希函數(shù)的工作量,本文就來詳細(xì)的介紹一下,感興趣的可以了解一下2023-08-08java如何調(diào)用kettle設(shè)置變量和參數(shù)
文章簡要介紹了如何在Java中調(diào)用Kettle,并重點(diǎn)討論了變量和參數(shù)的區(qū)別,以及在Java代碼中如何正確設(shè)置和使用這些變量,避免覆蓋Kettle中已設(shè)置的變量,作者分享了個(gè)人經(jīng)驗(yàn),并鼓勵(lì)大家參考和使用腳本之家2025-01-01關(guān)于spring中aop的注解實(shí)現(xiàn)方法實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于spring中aop的注解實(shí)現(xiàn)方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起看看吧。2017-08-08基于ChatGPT+SpringBoot實(shí)現(xiàn)智能聊天AI機(jī)器人接口并上線至服務(wù)器的方法
ChatGPT是一款基于自然語言處理技術(shù)的聊天機(jī)器人,ChatGPT可以模擬真實(shí)的人類對(duì)話,并能夠更貼近用戶的需求,提供更有價(jià)值的服務(wù),這篇文章主要介紹了基于ChatGPT+SpringBoot實(shí)現(xiàn)智能聊天AI機(jī)器人接口并上線至服務(wù)器,需要的朋友可以參考下2023-02-02springboot集成swagger3與knife4j的詳細(xì)代碼
這篇文章主要介紹了springboot集成swagger3與knife4j,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08