當我們想要封裝一些自定義功能給別人使用的時候,創(chuàng)建Spring Boot Starter的形式是最好的實現(xiàn)方式。如果您還不會構(gòu)建自己的Spring Boot Starter的話,本文將帶你一起創(chuàng)建一個自己的Spring Boot Starter。
快速入門
-
創(chuàng)建一個新的 Maven 項目。第三方封裝的命名格式是
xxx-spring-boot-starter
,例如:didispace-spring-boot-starter
。 -
編輯
pom.xml
,添加spring-boot-autoconfigure
和spring-boot-starter
依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
- 創(chuàng)建一個用
@Configuration
注釋的配置類,在這里您可以使用@Bean
來創(chuàng)建使用@ConditionalOnClass
、@ConditionalOnMissingBean
等條件注釋來控制何時應(yīng)用配置。
@Configuration
@ConditionalOnClass(MyFeature.class)
@ConditionalOnProperty(prefix = "myfeature", name = "enabled", matchIfMissing = true)
public class MyFeatureAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public MyFeature myFeature() {
return new MyFeature();
}
}
- 在
src/main/resources/META-INF
目錄下創(chuàng)建spring.factories
文件,并在org.springframework.boot.autoconfigure.EnableAutoConfiguration
關(guān)鍵字下列出您的自動配置類,比如:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.didispace.myfeature.MyFeatureAutoConfiguration
該配置的作用是讓Spring Boot應(yīng)用在引入您自定義Starter的時候可以自動這里的配置類。
注意:Spring Boot 2.7開始,不再推薦使用
spring.factories
,而是改用/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
,文件內(nèi)容直接放需要自動加載配置類路徑即可。這個變更具體可見之前的這篇文章:《Spring Boot 2.7開始spring.factories不推薦使用了》
驗證測試
在制作Spring Boot Starter的時候,一定記得使用單元測試來驗證和確保自動化配置類在任何條件邏輯在啟動器下能夠按照正確的預(yù)期運行。
創(chuàng)建單元測試
使用@SpringBootTest
加載完整的應(yīng)用程序上下文,并驗證啟動程序是否正確配置了 Bean 和屬性。
@SpringBootTest(classes = TestApplication.class)
public class MyStarterAutoConfigurationTest {
@Autowired(required = false)
private MyService myService;
@Test
public void testMyServiceAutoConfigured() {
assertNotNull(myService, "MyService should be auto-configured");
}
}
覆蓋不同的配置
如果有不同的配置方案,那么還需要使用@TestPropertySource
或@DynamicPropertySource
覆蓋屬性以測試不同配置下的情況。
或者也可以直接簡單的通過@SpringBootTest
中的屬性來配置,比如下面這樣:
@SpringBootTest(properties = "my.starter.custom-property=customValue")
public class MyStarterPropertiesTest {
@Value("${my.starter.custom-property}")
private String customProperty;
@Test
public void testPropertyOverride() {
assertEquals("customValue", customProperty, "Custom property should be overridden by @SpringBootTest");
}
}
覆蓋@Conditional的不同分支
如果您的啟動器包含條件配置,比如:@ConditionalOnProperty
、@ConditionalOnClass
等注解,那么就必須編寫測試來覆蓋所有條件以驗證是否已正確。
比如下面這樣:
@SpringBootTest(classes = {TestApplication.class, MyConditionalConfiguration.class})
@ConditionalOnProperty(name = "my.starter.enable", havingValue = "true")
public class MyStarterConditionalTest {
@Autowired
private ApplicationContext context;
@Test
public void conditionalBeanNotLoadedWhenPropertyIsFalse() {
assertFalse(
context.containsBean("conditionalBean"),
"Conditional bean should not be loaded when 'my.starter.enable' is false"
);
}
}
為了覆蓋不同的條件分支,我們通常還需要使用@TestConfiguration
注解來有選擇地啟用或禁用某些自動配置。
小結(jié)
本文介紹了兩個Spring Boot的進階內(nèi)容:
- 如何創(chuàng)建 Spring Boot Starter
- 如何為 Spring Boot Starter 提供單元測試
掌握這項技能可以幫你更好的為Spring Boot提供模塊劃的功能封裝。如果您學(xué)習過程中如遇困難?可以加入我們超高質(zhì)量的Spring技術(shù)交流群,參與交流與討論,更好的學(xué)習與進步!更多Spring Boot教程可以點擊直達!,歡迎收藏與轉(zhuǎn)發(fā)支持!
最后再給大家推薦一些有關(guān)Spring Boot Starter和自動化配置的擴展閱讀:文章來源:http://www.zghlxwxcb.cn/news/detail-840390.html
- Spring Boot Starter配置spring.factories的自動生成神器
- Spring Boot自動化配置的利弊及解決之道
歡迎關(guān)注我的公眾號:程序猿DD。前沿技術(shù)早知道,彎道超車有希望!積累超車資本,從關(guān)注DD開始!文章來源地址http://www.zghlxwxcb.cn/news/detail-840390.html
到了這里,關(guān)于如何創(chuàng)建自己的Spring Boot Starter并為其編寫單元測試的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!