Custom Spring Boot Starter
Introduction
Teams repeat the same integration code across projects—logging format, auth filters, or a shared client SDK. A custom Spring Boot Starter packages auto-configuration and default properties so other apps add one dependency and configure a prefix. This chapter builds a minimal demo-greeting-starter module structure you can adapt for internal libraries.
Prerequisites
- Auto-Configuration Mechanism
- Configuration Properties and Type-Safe Config
- Multi-module Maven basics from Maven track
Starter Module Layout
Typical two-module design:
demo-greeting/
├── demo-greeting-spring-boot-autoconfigure/
│ └── src/main/java + META-INF/spring/...
└── demo-greeting-spring-boot-starter/
└── pom.xml (depends on autoconfigure + optional deps)| Module | Purpose |
|---|---|
| autoconfigure | @AutoConfiguration, @ConfigurationProperties, conditions |
| starter | Thin POM consumers import—pulls autoconfigure transitively |
Apps depend only on demo-greeting-spring-boot-starter.
Properties Class
Service Bean
Auto-Configuration Class
Register in Boot 3:
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.importsFile content:
com.example.greeting.GreetingAutoConfigurationCode explanation:
@ConditionalOnMissingBeanlets apps override with customGreetingServicematchIfMissing = trueenables starter by default
Starter POM (Consumer Dependency)
<dependency>
<groupId>com.example</groupId>
<artifactId>demo-greeting-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>Consumer application.yml:
demo:
greeting:
prefix: HiConsumer controller:
Optional: spring-configuration-metadata.json
For IDE hints, add META-INF/additional-spring-configuration-metadata.json documenting demo.greeting.* keys.
Publishing Internally
mvn clean installto local repo for dev- Publish to company Nexus/Artifactory for teams
- Version starters SemVer—breaking property renames = major bump
Warning
Keep Starters Focused
One starter = one capability. Do not bundle unrelated auto-config into a "kitchen sink" starter.
FAQ
spring.factories still needed?
Boot 3 uses AutoConfiguration.imports only for auto-config registration.
Starter without autoconfigure module?
Small libs can use one JAR—split when autoconfigure grows.
Test auto-config?
Use @ImportAutoConfiguration(GreetingAutoConfiguration.class) in slice tests.
Conflict with official starter?
Use unique prefix demo.greeting—never hijack spring.*.
Gradle multi-module?
Same layout with include subprojects—see Gradle track.
Disable starter?
demo.greeting.enabled=false or exclude auto-config class.