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

Starter Module Layout

Typical two-module design:

text
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)
ModulePurpose
autoconfigure@AutoConfiguration, @ConfigurationProperties, conditions
starterThin 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:

text
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

File content:

text
com.example.greeting.GreetingAutoConfiguration

Code explanation:

  • @ConditionalOnMissingBean lets apps override with custom GreetingService
  • matchIfMissing = true enables starter by default

Starter POM (Consumer Dependency)

xml
<dependency>
    <groupId>com.example</groupId>
    <artifactId>demo-greeting-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

Consumer application.yml:

yaml
demo:
  greeting:
    prefix: Hi

Consumer controller:

Optional: spring-configuration-metadata.json

For IDE hints, add META-INF/additional-spring-configuration-metadata.json documenting demo.greeting.* keys.

Publishing Internally

  1. mvn clean install to local repo for dev
  2. Publish to company Nexus/Artifactory for teams
  3. 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.