Backend Development 5 min read

How to Build and Use a Custom Spring Boot Starter in Minutes

Learn step‑by‑step how to create a custom Spring Boot starter, configure its properties, define services and controllers, register it via spring.factories, and then integrate the starter into other projects with Maven dependencies and configuration files.

Lobster Programming
Lobster Programming
Lobster Programming
How to Build and Use a Custom Spring Boot Starter in Minutes

In Spring Boot we often see Maven coordinates like spring-boot-starter-web . Spring Boot extracts common scenarios into "starters" that can be added with minimal configuration to obtain the desired functionality.

Enterprises may wrap internal middleware as a starter so other projects can depend on it and get automatic configuration. The following guide shows how to create a custom starter.

1. Custom Starter

Requirement: Use a custom starter to provide configurable parameters for each project and read those values.

The project structure is illustrated below:

(1) Add necessary dependencies in the custom starter module:

<code>&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-autoconfigure&lt;/artifactId&gt;
    &lt;/dependency&gt;
    
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-autoconfigure-processor&lt;/artifactId&gt;
    &lt;/dependency&gt;
    
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;</code>

(2) Create a configuration class to read custom properties:

<code>@Component
@ConfigurationProperties("com.longxia")
@Data
public class MytConfigProperty {
    private String name;
    private Integer age;
}</code>

(3) Define a service that uses the configuration:

<code>public class MyService {
    private MyConfigProperty myConfigProperty;

    public MyService() {}

    public MyService(MytConfigProperty myConfigProperty) {
        this.myConfigProperty = myConfigProperty;
    }

    public String say() {
        return "myStarter " + myConfigProperty.getName() + " !";
    }
}</code>

(4) Add service configuration:

<code>@Configuration
@EnableConfigurationProperties({MytConfigProperty.class})
public class MyConfig {
    @Resource
    private MyConfigProperty myConfigProperty;

    @Bean
    public MyFirstService getMyFirstService() {
        return new MyFirstService(myFirstConfigProperty);
    }
}</code>

(5) Write spring.factories to make the starter discoverable:

<code>org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.longxia.spring.starter.config.MyConfig</code>

The purpose of spring.factories is to let Spring Boot scan and load the custom starter during application startup.

2. Use the Custom Starter

(1) Add the custom starter as a Maven dependency:

<code>&lt;dependency&gt;
    &lt;groupId&gt;com.longxia.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;common-starter&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
&lt;/dependency&gt;</code>

(2) Add configuration property values in the consuming project's configuration file (example shown below):

(3) Define a controller to test the starter:

<code>@RestController
@RequestMapping("/starter")
public class MyFirstStarterController {
    @Resource
    private MytService myService;

    @GetMapping("/first")
    public String first() {
        return mytService.hello();
    }
}</code>

Access the endpoint in a browser at http://localhost:8080/starter/first and you will see the response, as shown in the screenshot below:

With these steps, the custom starter is complete. Any business module that needs the encapsulated common-starter can simply add its Maven dependency and the corresponding configuration to start using it.

JavaconfigurationMavenSpring BootCustom Starter
Lobster Programming
Written by

Lobster Programming

Sharing insights on technical analysis and exchange, making life better through technology.

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.