关于java:如何创建自己的Spring-Boot-Starter并为其编写单元测试

当咱们想要封装一些自定义性能给他人应用的时候,创立Spring Boot Starter的模式是最好的实现形式。如果您还不会构建本人的Spring Boot Starter的话,本文将带你一起创立一个本人的Spring Boot Starter。

疾速入门

  1. 创立一个新的 Maven 我的项目。第三方封装的命名格局是 xxx-spring-boot-starter ,例如:didispace-spring-boot-starter
  2. 编辑pom.xml,增加spring-boot-autoconfigurespring-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>
  1. 创立一个用 @Configuration 正文的配置类,在这里您能够应用@Bean来创立应用@ConditionalOnClass@ConditionalOnMissingBean等条件正文来管制何时利用配置。
@Configuration
@ConditionalOnClass(MyFeature.class)
@ConditionalOnProperty(prefix = "myfeature", name = "enabled", matchIfMissing = true)
public class MyFeatureAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public MyFeature myFeature() {
        return new MyFeature();
    }
}
  1. src/main/resources/META-INF目录下创立spring.factories文件,并在org.springframework.boot.autoconfigure.EnableAutoConfiguration关键字下列出您的主动配置类,比方:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.didispace.myfeature.MyFeatureAutoConfiguration

该配置的作用是让Spring Boot利用在引入您自定义Starter的时候能够主动这里的配置类。

留神:Spring Boot 2.7开始,不再举荐应用spring.factories,而是改用/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports,文件内容间接放须要主动加载配置类门路即可。这个变更具体可见之前的这篇文章:《Spring Boot 2.7开始spring.factories不举荐应用了》

验证测试

在制作Spring Boot Starter的时候,肯定记得应用单元测试来验证和确保自动化配置类在任何条件逻辑在启动器下可能依照正确的预期运行。

创立单元测试

应用@SpringBootTest加载残缺的应用程序上下文,并验证启动程序是否正确配置了 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注解来有选择地启用或禁用某些主动配置。

小结

本文介绍了两个Spring Boot的进阶内容:

  1. 如何创立 Spring Boot Starter
  2. 如何为 Spring Boot Starter 提供单元测试

把握这项技能能够帮你更好的为Spring Boot提供模块划的性能封装。如果您学习过程中如遇艰难?能够退出咱们超高品质的Spring技术交换群,参加交换与探讨,更好的学习与提高!更多Spring Boot教程能够点击中转!,欢送珍藏与转发反对!

最初再给大家举荐一些无关Spring Boot Starter和自动化配置的扩大浏览:

  • Spring Boot Starter配置spring.factories的主动生成神器
  • Spring Boot自动化配置的利弊及解决之道

欢送关注我的公众号:程序猿DD。第一工夫理解前沿行业音讯、分享深度技术干货、获取优质学习资源

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理