使用SpringBoot自定義starter的完整步驟
前言
使用過SpringBoot的都應(yīng)該知道,一個SpringBoot 項(xiàng)目就是由一個一個 Starter 組成的,一個 Starter 代表該項(xiàng)目的 SpringBoot 啟動依賴,除了官方已有的 Starter,我們可以根據(jù)自己的需要自定義新的Starter。
一、自定義SpringBoot Starter
自定義Starter,首選需要實(shí)現(xiàn)自動化配置,而要實(shí)現(xiàn)自動化配置需要滿足以下兩個條件:
(1)能夠自動配置項(xiàng)目所需要的配置信息,也就是自動加載依賴環(huán)境;
(2)能夠根據(jù)項(xiàng)目提供的信息自動生成Bean,并且注冊到Bean管理容器中;
要實(shí)現(xiàn)自動化配置需要在項(xiàng)目的pom.xml文件中引入如下依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.1.4.RELEASE</version> </dependency>
根據(jù)需要自定義Starter的實(shí)現(xiàn)過程大致如下(以我定義的Starter為例):
工程目錄結(jié)構(gòu):

1、引入項(xiàng)目的配置依賴
<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
//指定項(xiàng)目在屬性文件中配置的前綴為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ù)此文件來加載項(xiàng)目的自動化配置類
代碼:spring.factories
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.lhf.springboot.config.StringAutoConfiguration
6、到這里自定義Starter就定義完成了,只需在其他項(xiàng)目中引入即可使用。
二、其他項(xiàng)目中使用自定義的Starter
1、在新項(xiàng)目中引入自定義Starter依賴配置
創(chuàng)建一個新的SpringBoot項(xiàng)目,在項(xiàng)目的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=那是因?yàn)槲覍δ銗鄣纳畛?
4、啟動項(xiàng)目進(jìn)行訪問,效果如圖:


結(jié)語:
到此這篇關(guān)于使用SpringBoot自定義starter的文章就介紹到這了,更多相關(guān)SpringBoot自定義starter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot項(xiàng)目中結(jié)合MyBatis實(shí)現(xiàn)MySQL的自動主從切換功能
這篇文章主要介紹了Spring Boot項(xiàng)目中結(jié)合MyBatis實(shí)現(xiàn)MySQL的自動主從切換功能,本文分步驟給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-04-04
Java 字符串轉(zhuǎn)float運(yùn)算 float轉(zhuǎn)字符串的方法
今天小編就為大家分享一篇Java 字符串轉(zhuǎn)float運(yùn)算 float轉(zhuǎn)字符串的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
springboot自動掃描添加的BeanDefinition源碼實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于springboot自動掃描添加的BeanDefinition的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-02-02
Java 位運(yùn)算符>>與>>>區(qū)別案例詳解
這篇文章主要介紹了Java 位運(yùn)算符>>與>>>區(qū)別案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
springboot集成spark并使用spark-sql的示例詳解
這篇文章主要介紹了spring-boot集成spark并使用spark-sql的方法,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02
SpringBoot整合logback日志的詳細(xì)步驟
這篇文章主要介紹了SpringBoot整合logback日志的詳細(xì)步驟,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05
Spring中的EventListenerMethodProcessor組件詳解
這篇文章主要介紹了Spring中的EventListenerMethodProcessor組件詳解,EventListenerMethodProcessor 是 Spring 事件機(jī)制中非常重要的一個組件,它管理了一組EventListenerFactory組件,用來將應(yīng)用中每個使用@EventListener注解定義的事件監(jiān)聽,需要的朋友可以參考下2023-12-12

