How to Build Your Own Spring Boot Starter: A Step‑by‑Step Guide
This tutorial walks you through creating a custom Spring Boot Starter, from Maven project setup and dependency configuration to defining auto‑configuration classes, registering them, and writing comprehensive unit tests to verify conditional behavior.
Quick Start
Create a new Maven project. Name it xxx-spring-boot-starter, e.g., didispace-spring-boot-starter.
Edit pom.xml and add spring-boot-autoconfigure and spring-boot-starter dependencies.
<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>Create Configuration Class
Define a class annotated with @Configuration and use @Bean, @ConditionalOnClass, @ConditionalOnMissingBean etc. to control bean creation.
@Configuration
@ConditionalOnClass(MyFeature.class)
@ConditionalOnProperty(prefix="myfeature", name="enabled", matchIfMissing=true)
public class MyFeatureAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public MyFeature myFeature() {
return new MyFeature();
}
}Register Auto‑Configuration
Under src/main/resources/META-INF create spring.factories and list the auto‑configuration class:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.didispace.myfeature.MyFeatureAutoConfigurationNote: Starting with Spring Boot 2.7, spring.factories is deprecated; use META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports instead.
Validation Testing
Write unit tests with @SpringBootTest to load the full application context and verify beans and properties.
@SpringBootTest(classes = TestApplication.class)
public class MyStarterAutoConfigurationTest {
@Autowired(required = false)
private MyService myService;
@Test
public void testMyServiceAutoConfigured() {
assertNotNull(myService, "MyService should be auto-configured");
}
}Use @TestPropertySource or @DynamicPropertySource to override properties, or set them directly in @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");
}
}For conditional configurations, add tests that cover each branch, optionally using @TestConfiguration to enable or disable specific auto‑configurations.
Conclusion
The article covered two advanced Spring Boot topics: creating a Spring Boot Starter and providing unit tests for it.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
