使用SpringBoot自定義starter的完整步驟
前言
使用過SpringBoot的都應(yīng)該知道,一個SpringBoot 項目就是由一個一個 Starter 組成的,一個 Starter 代表該項目的 SpringBoot 啟動依賴,除了官方已有的 Starter,我們可以根據(jù)自己的需要自定義新的Starter。
一、自定義SpringBoot Starter
自定義Starter,首選需要實現(xiàn)自動化配置,而要實現(xiàn)自動化配置需要滿足以下兩個條件:
(1)能夠自動配置項目所需要的配置信息,也就是自動加載依賴環(huán)境;
(2)能夠根據(jù)項目提供的信息自動生成Bean,并且注冊到Bean管理容器中;
要實現(xiàn)自動化配置需要在項目的pom.xml文件中引入如下依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.1.4.RELEASE</version> </dependency>
根據(jù)需要自定義Starter的實現(xiàn)過程大致如下(以我定義的Starter為例):
工程目錄結(jié)構(gòu):
1、引入項目的配置依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.1.4.RELEASE</version> </dependency>
2、創(chuàng)建xxxService類,完成相關(guān)的操作邏輯
代碼:StringService.java
public class StringService { private String str1; private String str2; private String default_str; public String getStr1() { return str1; } public void setStr1(String str1) { this.str1 = str1; } public String getStr2() { return str2; } public void setStr2(String str2) { this.str2 = str2; } public String getDefault_str() { return default_str; } public void setDefault_str(String default_str) { this.default_str = default_str; } public String addStr(){ if(str1 != null){ if(str2 != null){ return str1 + "," + str2; } return str1; } return default_str; } }
3、 定義xxxProperties類,屬性配置類,完成屬性配置相關(guān)的操作,比如設(shè)置屬性前綴,用于在application.properties中配置
代碼:StringProperties.java
//指定項目在屬性文件中配置的前綴為str,即可以在屬性文件中通過 str.str1=springboot,就可以改變屬性類字段 str1 的值了 @SuppressWarnings("ConfigurationProperties") @ConfigurationProperties(prefix = "str") public class StringProperties { public static final String DEFAULT_STR1 = "I know, you need me"; public static final String DEFAULT_STR2 = "but I also need you"; private String str1 = DEFAULT_STR1; private String str2 = DEFAULT_STR2; public String getStr1() { return str1; } public void setStr1(String str1) { this.str1 = str1; } public String getStr2() { return str2; } public void setStr2(String str2) { this.str2 = str2; } }
4、定義xxxConfigurationProperties類,自動配置類,用于完成Bean創(chuàng)建等工作
代碼:StringAutoConfiguration.java
// 定義 java 配置類 @Configuration //引入StringService @ConditionalOnClass({StringService.class}) // 將 application.properties 的相關(guān)的屬性字段與該類一一對應(yīng),并生成 Bean @EnableConfigurationProperties(StringProperties.class) public class StringAutoConfiguration { // 注入屬性類 @Autowired private StringProperties stringProperties; @Bean // 當容器沒有這個 Bean 的時候才創(chuàng)建這個 Bean @ConditionalOnMissingBean(StringService.class) public StringService helloworldService() { StringService stringService = new StringService(); stringService.setStr1(stringProperties.getStr1()); stringService.setStr2(stringProperties.getStr2()); return stringService; } }
5、在resources下創(chuàng)建目錄META-INF,在 META-INF 目錄下創(chuàng)建 spring.factories,在SpringBoot啟動時會根據(jù)此文件來加載項目的自動化配置類
代碼:spring.factories
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.lhf.springboot.config.StringAutoConfiguration
6、到這里自定義Starter就定義完成了,只需在其他項目中引入即可使用。
二、其他項目中使用自定義的Starter
1、在新項目中引入自定義Starter依賴配置
創(chuàng)建一個新的SpringBoot項目,在項目的pom.xml文件中引入自定義SpringBoot Starter的依賴配置如下:
<!--引入自定義Starter--> <dependency> <groupId>com.lhf.springboot</groupId> <artifactId>spring-boot-starter-string</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
2、編寫一個簡單的Controller
@RestController public class StringController { @Autowired private StringService stringService; //引入自定義Starter中的StringService @RequestMapping("/") public String addString(){ return stringService.addStr(); } }
3、編寫屬性配置文件,內(nèi)容如下:
#配置自定義的屬性信息 str.str1=為什么我的眼里常含淚水 str.str2=那是因為我對你愛的深沉
4、啟動項目進行訪問,效果如圖:
結(jié)語:
到此這篇關(guān)于使用SpringBoot自定義starter的文章就介紹到這了,更多相關(guān)SpringBoot自定義starter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Idea如何導入一個SpringBoot項目的方法(圖文教程)
這篇文章主要介紹了Idea如何導入一個SpringBoot項目的方法(圖文教程),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09Java中ArrayList具體實現(xiàn)之簡單的洗牌算法
這篇文章主要給大家介紹了Java中ArrayList具體實現(xiàn)之簡單的洗牌算法,文中通過代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-12-12Spring?Boot中的max-http-header-size配置方式
這篇文章主要介紹了Spring?Boot中的max-http-header-size配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09