Backend Development 10 min read

Creating a Custom Spring Boot Starter: Overview, Auto‑Configuration, and Usage

This tutorial explains how to build a custom Spring Boot starter by detailing the auto‑configuration mechanism, loading custom properties, creating the starter and its pom files, and demonstrating its use in a sample application to simplify configuration and bean creation.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Creating a Custom Spring Boot Starter: Overview, Auto‑Configuration, and Usage

Spring Boot provides many starter modules for popular open‑source projects, but developers can also create their own custom starters to encapsulate internal libraries and simplify configuration within the Spring Boot context.

1. Overview

Custom starters avoid repetitive setup and enable rapid development; this article uncovers Spring Boot's inner workings and shows how to create a starter for a personal greeting library.

2. Unveiling Spring Boot Auto‑Configuration

2.1 Auto‑Configuration Classes

Since version 2.7, Spring Boot scans META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports for fully‑qualified auto‑configuration class names, such as org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration . Earlier versions use META-INF/spring.factories .

<code>org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
</code>

Conditional initialization relies on @ConditionalOnClass ; for example, MongoAutoConfiguration runs only if MongoClient is present on the classpath.

<code>@Configuration
@ConditionalOnClass(MongoClient.class)
@EnableConfigurationProperties(MongoProperties.class)
@ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDbFactory")
public class MongoAutoConfiguration {
    // configuration code
}
</code>

2.2 Loading Custom Properties from application.properties

Properties prefixed with the class name (e.g., spring.data.mongodb ) are bound to @ConfigurationProperties classes like MongoProperties .

<code>@ConfigurationProperties(prefix = "spring.data.mongodb")
public class MongoProperties {
    private String host;
    // getters and setters
}
</code>

Setting spring.data.mongodb.host = localhost in application.properties configures the MongoDB host.

3. Building a Custom Starter

To create a starter, we need an auto‑configuration class, a properties class, a starter pom, and registration files.

3.1 Auto‑Configuration Module

The auto‑configuration module greeter-spring-boot-autoconfigure defines GreeterProperties and GreeterAutoConfiguration .

<code>@ConfigurationProperties(prefix = "baeldung.greeter")
public class GreeterProperties {
    private String userName;
    private String morningMessage;
    private String afternoonMessage;
    private String eveningMessage;
    private String nightMessage;
    // getters and setters
}
</code>
<code>@Configuration
@ConditionalOnClass(Greeter.class)
@EnableConfigurationProperties(GreeterProperties.class)
public class GreeterAutoConfiguration {

    @Autowired
    private GreeterProperties greeterProperties;

    @Bean
    @ConditionalOnMissingBean
    public GreetingConfig greeterConfig() {
        String userName = greeterProperties.getUserName() == null
            ? System.getProperty("user.name")
            : greeterProperties.getUserName();
        GreetingConfig greetingConfig = new GreetingConfig();
        greetingConfig.put(USER_NAME, userName);
        return greetingConfig;
    }

    @Bean
    @ConditionalOnMissingBean
    public Greeter greeter(GreetingConfig greetingConfig) {
        return new Greeter(greetingConfig);
    }
}
</code>

The fully‑qualified name com.baeldung.greeter.autoconfigure.GreeterAutoConfiguration is added to META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports (or spring.factories for pre‑2.7 versions).

3.2 Creating pom.xml for the Starter

<code>&lt;project ...&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.baeldung&lt;/groupId&gt;
    &lt;artifactId&gt;greeter-spring-boot-starter&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
            &lt;version&gt;${spring-boot.version}&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.baeldung&lt;/groupId&gt;
            &lt;artifactId&gt;greeter-spring-boot-autoconfigure&lt;/artifactId&gt;
            &lt;version&gt;${project.version}&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.baeldung&lt;/groupId&gt;
            &lt;artifactId&gt;greeter&lt;/artifactId&gt;
            &lt;version&gt;${greeter.version}&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
    &lt;properties&gt;
        &lt;greeter.version&gt;0.0.1-SNAPSHOT&lt;/greeter.version&gt;
    &lt;/properties&gt;
&lt;/project&gt;
</code>

3.3 Using the Starter

In a sample application, add the starter as a dependency:

<code>&lt;dependency&gt;
    &lt;groupId&gt;com.baeldung&lt;/groupId&gt;
    &lt;artifactId&gt;greeter-spring-boot-starter&lt;/artifactId&gt;
    &lt;version&gt;${greeter-starter.version}&lt;/version&gt;
&lt;/dependency&gt;
</code>

Define custom properties in application.properties :

<code>baeldung.greeter.userName=Baeldung
baeldung.greeter.afternoonMessage=Woha\ Afternoon
</code>

Inject and use the Greeter bean:

<code>@SpringBootApplication
public class GreeterSampleApplication implements CommandLineRunner {

    @Autowired
    private Greeter greeter;

    public static void main(String[] args) {
        SpringApplication.run(GreeterSampleApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        String message = greeter.greet();
        System.out.println(message);
    }
}
</code>

4. Conclusion

The guide demonstrates how to publish a custom Spring Boot starter and how its auto‑configuration simplifies configuration, reducing manual setup for developers.

JavaSpring Bootdependency injectionAuto‑ConfigurationCustom Starter
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.