Learnitweb

Create a custom starter in Spring Boot

Creating a custom Spring Boot starter is a powerful way to package reusable configuration, dependencies, and auto-configuration logic into a single module. This is especially useful when you want to apply common setups across multiple Spring Boot projects in your organization or team.

What Is a Spring Boot Starter?

A starter is a special kind of dependency that:

  • Bundles other dependencies (via pom.xml)
  • Optionally auto-configures beans using @Configuration
  • Supports conditional configuration using @ConditionalOn...
  • Loads automatically if on the classpath, like spring-boot-starter-web, spring-boot-starter-data-jpa, etc.

Step-by-Step: Create a Custom Spring Boot Starter

1. Create a New Maven Module (Your Custom Starter)

Create a new Maven project, e.g., my-company-starter-logging.

pom.xml:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.starters</groupId>
  <artifactId>my-logging-starter</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>

  <name>My Custom Logging Starter</name>

  <properties>
    <java.version>17</java.version>
  </properties>

  <dependencies>
    <!-- Required Spring Boot Auto-Configuration -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>

    <!-- Optional: Bring in Spring Boot Starter Logging -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-logging</artifactId>
    </dependency>

    <!-- Optional: Add your own utility libraries -->
  </dependencies>
</project>

2. Create the Auto-Configuration Class

In src/main/java/com/mycompany/logging/autoconfig/LoggingAutoConfiguration.java:

package com.mycompany.logging.autoconfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;

@Configuration
public class LoggingAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public Logger myLogger() {
        return LoggerFactory.getLogger("MyCustomLogger");
    }
}

3. Register Auto-Configuration in spring.factories

Create this file:

src/main/resources/META-INF/spring.factories

Content:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.mycompany.logging.autoconfig.LoggingAutoConfiguration

Note: For Spring Boot 3.x, you should use spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports instead. But spring.factories still works for compatibility.

4. (Optional) Add Default Configuration Properties

You can define default properties in:

src/main/resources/application.properties

Or allow external properties by adding a @ConfigurationProperties class.

5. Publish Your Starter to a Maven Repository

You can:

  • Install locally: mvn clean install
  • Publish to Nexus, Artifactory, GitHub Packages, or Maven Central

Using the Custom Starter in Other Projects

In your target Spring Boot app’s pom.xml:

<dependency>
  <groupId>com.mycompany.starters</groupId>
  <artifactId>my-logging-starter</artifactId>
  <version>1.0.0</version>
</dependency>

Spring Boot will automatically apply your configuration if it’s available on the classpath.

Optional Features You Can Add to Your Starter

FeatureHow to Add
Default properties@ConfigurationProperties
Conditional logic@ConditionalOnClass, @ConditionalOnProperty
Runtime profile support@Profile("dev")
Custom annotationsCreate and use annotations in config
Enable/Disable via property@ConditionalOnProperty

Example Use Cases for Custom Starters

Use CaseStarter Name
Standardized logging configurationmy-logging-starter
Custom error handlingmy-error-handler-starter
Integrate with internal servicesinternal-auth-starter
Set up monitoring and alertsmonitoring-metrics-starter
Predefined database configurationscustom-jpa-starter

Testing Your Starter

In a test Spring Boot app:

  1. Add your starter as a dependency.
  2. Start the application.
  3. Verify that the beans from your starter are auto-loaded.
  4. Check logs or endpoints.