Table of Contents
- Introduction to Spring Boot Starters
- Why Create a Custom Starter?
- Anatomy of a Spring Boot Starter
- Creating the Starter Module
- Defining Auto-Configuration
- Using
@Conditional
Annotations - Registering the Auto-Configuration Class
- Publishing Your Starter
- Using Your Starter in a Project
- Best Practices
- Summary
1. Introduction to Spring Boot Starters
Spring Boot starters are dependency descriptors that bundle together related libraries, configurations, and auto-configuration logic. For instance, when you add spring-boot-starter-web
, it pulls in all dependencies needed to build a web application and configures them automatically.
A custom Spring Boot starter allows you to encapsulate common functionality (libraries, configurations, and auto-configs) and share it across multiple Spring Boot projects.
2. Why Create a Custom Starter?
Creating a custom starter is useful when:
- You want to standardize configuration across microservices.
- You repeatedly use the same set of dependencies and configuration classes.
- You are developing an internal or third-party SDK with Spring Boot support.
3. Anatomy of a Spring Boot Starter
A typical custom starter project has two Maven modules:
- Autoconfigure Module – Contains the configuration logic.
- Starter Module – Brings in the autoconfigure module and defines the starter dependency.
4. Creating the Starter Module
Step 1: Set Up Two Maven Modules
my-custom-starter-autoconfigure
my-custom-starter
Directory structure:
perlCopyEditmy-custom-starter/
├── my-custom-starter/
│ └── pom.xml
├── my-custom-starter-autoconfigure/
│ └── pom.xml
└── pom.xml
5. Defining Auto-Configuration
In the my-custom-starter-autoconfigure
module:
Add Required Dependencies
xmlCopyEdit<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
Create a Configuration Class
javaCopyEdit@Configuration
@ConditionalOnClass(MyService.class)
@EnableConfigurationProperties(MyServiceProperties.class)
public class MyServiceAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public MyService myService(MyServiceProperties properties) {
return new MyService(properties.getMessage());
}
}
Properties Class
javaCopyEdit@ConfigurationProperties("my.service")
public class MyServiceProperties {
private String message = "Hello from MyService";
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
}
6. Using @Conditional
Annotations
Spring Boot’s conditional annotations make autoconfiguration smart:
@ConditionalOnClass
: Only configure if class is present.@ConditionalOnMissingBean
: Only create bean if it’s not already defined.@ConditionalOnProperty
: Enable config based on property value.
Example:
javaCopyEdit@Bean
@ConditionalOnProperty(name = "my.service.enabled", havingValue = "true", matchIfMissing = true)
public MyService myService() {
return new MyService();
}
7. Registering the Auto-Configuration Class
You must register the auto-configuration class using the spring.factories
file.
In:src/main/resources/META-INF/spring.factories
Add:
CopyEditorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.autoconfig.MyServiceAutoConfiguration
8. Publishing Your Starter
Package both modules and publish to your internal or public Maven repository.
bashCopyEditmvn clean install
# or publish to remote repo
9. Using Your Starter in a Project
Now you can add your starter in another project:
xmlCopyEdit<dependency>
<groupId>com.example</groupId>
<artifactId>my-custom-starter</artifactId>
<version>1.0.0</version>
</dependency>
And optionally configure it in application.properties
:
propertiesCopyEditmy.service.enabled=true
my.service.message=Hello from Custom Starter
10. Best Practices
- Keep logic in the autoconfigure module; starter should be a thin wrapper.
- Use appropriate
@Conditional
annotations to avoid conflicts. - Provide reasonable defaults in your properties classes.
- Document available properties for users of your starter.
11. Summary
Creating a custom Spring Boot starter streamlines repetitive setup and promotes consistent configurations across your projects. By encapsulating dependencies, auto-configurations, and custom properties, you make your reusable functionality portable and production-ready.