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.
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><dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies></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><dependency>
<groupId>com.longxia.cloud</groupId>
<artifactId>common-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency></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.
Lobster Programming
Sharing insights on technical analysis and exchange, making life better through technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.