使用SpringBoot自定義starter的完整步驟
前言
使用過SpringBoot的都應該知道,一個SpringBoot 專案就是由一個一個 Starter 組成的,一個 Starter 代表該專案的 SpringBoot 啟動依賴,除了官方已有的 Starter,我們可以根據自己的需要自定義新的Starter。
一、自定義SpringBoot Starter
自定義Starter,首選需要實現自動化配置,而要實現自動化配置需要滿足以下兩個條件:
(1)能夠自動配置專案所需要的配置資訊,也就是自動載入依賴環境;
(2)能夠根據專案提供的資訊自動生成Bean,並且註冊到Bean管理容器中;
要實現自動化配置需要在專案的pom.xml檔案中引入如下依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.1.4.RELEASE</version> </dependency>
根據需要自定義Starter的實現過程大致如下(以我定義的Starter為例):
工程目錄結構:
1、引入專案的配置依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.1.4.RELEASE</version> </dependency>
2、建立xxxService類,完成相關的操作邏輯
程式碼: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類,屬性配置類,完成屬性配置相關的操作,比如設定屬性字首,用於在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建立等工作
程式碼:StringAutoConfiguration.java
// 定義 java 配置類 @Configuration //引入StringService @ConditionalOnClass({StringService.class}) // 將 application.properties 的相關的屬性欄位與該類一一對應,並生成 Bean @EnableConfigurationProperties(StringProperties.class) public class StringAutoConfiguration { // 注入屬性類 @Autowired private StringProperties stringProperties; @Bean // 當容器沒有這個 Bean 的時候才建立這個 Bean @ConditionalOnMissingBean(StringService.class) public StringService helloworldService() { StringService stringService = new StringService(); stringService.setStr1(stringProperties.getStr1()); stringService.setStr2(stringProperties.getStr2()); return stringService; } }
5、在resources下建立目錄META-INF,在 META-INF 目錄下建立 spring.factories,在SpringBoot啟動時會根據此檔案來載入專案的自動化配置類
程式碼:spring.factories
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.lhf.springboot.config.StringAutoConfiguration
6、到這裡自定義Starter就定義完成了,只需在其他專案中引入即可使用。
二、其他專案中使用自定義的Starter
1、在新專案中引入自定義Starter依賴配置
建立一個新的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、編寫屬性配置檔案,內容如下:
#配置自定義的屬性資訊 str.str1=為什麼我的眼裡常含淚水 str.str2=那是因為我對你愛的深沉
4、啟動專案進行訪問,效果如圖:
結語:
到此這篇關於使用SpringBoot自定義starter的文章就介紹到這了,更多相關SpringBoot自定義starter內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!