使用過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
????//?當(dāng)容器沒有這個?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é)語:
到此SpringBoot自定義Starter的過程以及用法就完了笨奠,分享就到這里了!如果是有需要Java學(xué)習(xí)資料的般婆,可以點擊進入,暗號:jsgg乡范,各個方面的資料都有,免費提供晋辆!大家一起學(xué)習(xí)進步。