springboot自定義starter方法及注解實例
SpringBoot starter
用了springboot 那么久了居然都還沒自定義過starter,想想都覺得羞愧,所以今天來玩一下。
SpringBoot中的starter是一種非常重要的機制,能夠拋棄以前繁雜的配置,將其統(tǒng)一集成進starter,應用者只需要在maven中引入starter依賴,SpringBoot就能自動掃描到要加載的信息并啟動相應的默認配置。starter讓我們擺脫了各種依賴庫的處理,需要配置各種信息的困擾。
SpringBoot會自動通過classpath路徑下的類發(fā)現(xiàn)需要的Bean,并注冊進IOC容器。SpringBoot提供了針對日常企業(yè)應用研發(fā)各種場景的spring-boot-starter依賴模塊。所有這些依賴模塊都遵循著約定成俗的默認配置,并允許我們調(diào)整這些配置,即遵循“約定大于配置”的理念。
自定義starter
日常工作中有時有一些獨立于業(yè)務之外的功能或模塊,可能這個項目在用,另一個項目也要用,如果每次都重新集成的話就會很麻煩,這時我們只要把這些功能或模塊封裝成一個個starter的話,在使用的時候引入進去就很方便了。
自定義starter步驟
其實自定義starter很簡單,大致需要以下5步:
- 新建兩個模塊,命名規(guī)范: springboot自帶的starter命名規(guī)范為spring-boot-starter-xxx, 自定義的starter命名規(guī)范為xxx-spring-boot-starter
xxx-spring-boot-autoconfigure:自動配置核心代碼
xxx-spring-boot-starter:管理依賴
如果不需要將自動配置代碼和依賴項管理分離開來,則可以將它們組合到一個模塊中。只不過springboot
官方建議將兩個模塊分開。
- 2. 引入spring-boot-autoconfigure依賴
- 3. 創(chuàng)建自定義的XXXProperties 類: 這個類的屬性根據(jù)需要是要出現(xiàn)在配置文件中的。
- 4. 創(chuàng)建自定義的XXXAutoConfiguration類:這個類要配置自動配置時的一些邏輯,同時也要讓XXXProperties 類生效。
- 5. 創(chuàng)建自定義的spring.factories文件:在resources/META-INF創(chuàng)建一個spring.factories文件和spring-configuration-metadata.json,spring-configuration-metadata.json文件是用于在填寫配置文件時的智能提示,可要可不要,有的話提示起來更友好。spring.factories用于導入自動配置類,必須要有
實現(xiàn)
我這里為了方便就只創(chuàng)建一個模塊了,
- 創(chuàng)建一個模塊,命名為spring-boot-starter-myStarter,對應pom文件
<groupId>com.example</groupId> <artifactId>spring-boot-starter-myStarter</artifactId> <version>1.0</version> <name>my-starter</name>
- 引入spring-boot-autoconfigure依賴 我這里使用的spring-boot-autoconfigure版本是2.6.2
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.6.2</version> </dependency> </dependencies>
- 創(chuàng)建自定義的XXXProperties 類
@ConfigurationProperties(prefix = "com.arron")
public class MyStarterProperties {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
再創(chuàng)建一個MyStarterConfig用于讀取MyStarterProperties 里的屬性
public class MyStarterConfig {
private MyStarterProperties myStarterProperties;
private String name;
public MyStarterConfig(MyStarterProperties myStarterProperties) {
this.myStarterProperties = myStarterProperties;
}
public String getName() {
return myStarterProperties.getName();
}
public void setName(String name) {
this.name = name;
}
}
- 創(chuàng)建自定義的XXXAutoConfiguration類
@Configuration
// EnableConfigurationProperties value數(shù)組中的配置類起作用
@EnableConfigurationProperties(value = {MyStarterProperties.class})
public class MyStarterAutoConfiguration {
@Autowired
private MyStarterProperties myStarterProperties;
@Bean
@ConditionalOnMissingBean(MyStarterConfig.class)
public MyStarterConfig myStarterConfig(){
return new MyStarterConfig(myStarterProperties);
}
}
- 在resources/META-INF創(chuàng)建一個spring.factories文件
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.myStarter.MyStarterAutoConfiguration
spring-configuration-metadata.json
{
"group": [
{
"name": "com.arron",
"type": "com.example.myStarter.MyStarterProperties",
"sourceType": "com.example.myStarter.MyStarterProperties"
}
],
"properties": [
{
"name": "com.arron.name",
"type": "java.lang.String",
"description": "my start name",
"sourceType": "com.example.myStarter.MyStarterProperties",
"defaultValue": "MyStarterProperties name"
}
]
}
打包測試
找到如圖maven,點擊install,安裝到本地

然后新建一個項目導包進行測試,創(chuàng)建項目過程就不介紹了。
- 引入依賴
<dependency>
<groupId>com.example</groupId>
<artifactId>spring-boot-starter-myStarter</artifactId>
<version>1.0</version>
</dependency>
- 配置文件添加屬性:
com:
arron:
name: myname
- 單元測試:
@RunWith(SpringRunner.class)
@SpringBootTest
class RabbitmqApplicationTests {
@Autowired
private MyStarterConfig myStarterConfig;
@Test
public void testMyStarter(){
String name = myStarterConfig.getName();
System.out.println(name);
}
}
控制臺輸出:
myname
至此,一個簡單自定義的springboot starter就完成了。
注解解釋
下面這些注解在自定義starter是可能會用到。
- @Conditional:按照一定的條件進行判斷,滿足條件給容器注冊bean
- @ConditionalOnMissingBean:給定的在bean不存在時,則實例化當前Bean
- @ConditionalOnProperty:配置文件中滿足定義的屬性則創(chuàng)建bean,否則不創(chuàng)建
- @ConditionalOnBean:給定的在bean存在時,則實例化當前Bean
- @ConditionalOnClass: 當給定的類名在類路徑上存在,則實例化當前Bean
- @ConditionalOnMissingClass :當給定的類名在類路徑上不存在,則實例化當前Bean
以上就是springboot自定義starter方法詳解的詳細內(nèi)容,更多關于springboot自定義starter的資料請關注腳本之家其它相關文章!
相關文章
基于Rest的API解決方案(jersey與swagger集成)
下面小編就為大家?guī)硪黄赗est的API解決方案(jersey與swagger集成)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
項目打包成jar后包無法讀取src/main/resources下文件的解決
本文主要介紹了項目打包成jar后包無法讀取src/main/resources下文件的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04
詳解spring cloud整合Swagger2構建RESTful服務的APIs
這篇文章主要介紹了詳解spring cloud整合Swagger2構建RESTful服務的APIs,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
使用Springboot搭建OAuth2.0 Server的方法示例
這篇文章主要介紹了使用Springboot搭建OAuth2.0 Server的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08

