Creating and Customizing a Spring Boot Starter with Auto‑Configuration Explained
This article explains the problems of traditional Spring MVC setup, introduces Spring Boot starters as a solution, details naming conventions and project structure for custom starters, and walks through the implementation of auto‑configuration classes, properties binding, and the spring.factories registration process.
Before Spring Boot, building a web project with Spring MVC required several manual steps: adding Spring MVC dependencies, registering DispatcherServlet in web.xml, creating springmvc-servlet.xml with HandlerMapping, HandlerAdapter and ViewResolver, and configuring a datasource and transaction manager in applicationContext.xml. These steps caused dependency‑management difficulties and cumbersome configuration.
Spring Boot starters address these pain points by aggregating the necessary dependencies into a single starter and allowing most configuration to be handled through simple property files, dramatically reducing boilerplate.
To create a custom starter, follow the naming convention xx-spring-boot-starter (e.g., saas-user-spring-boot-starter) and structure the project with two core classes: an xxxAutoConfiguration class that declares beans and injects an xxxProperties class, and the xxxProperties class that holds configurable fields bound via @ConfigurationProperties. The starter’s pom.xml must include spring-boot-autoconfigure and spring-boot-configuration-processor as dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>An example auto‑configuration class:
@Configuration
@EnableConfigurationProperties(xxxProperties.class)
public class xxxAutoConfiguration {
@Resource
private xxxProperties properties;
@Bean
public static BeanYouNeed beanYouNeed() {
BeanYouNeed bean = new BeanYouNeed();
bean.setField(properties.getField());
return bean;
}
}The corresponding properties class:
@ConfigurationProperties(prefix = "your.properties")
public class xxxProperties {
private String url;
private String key;
private String secret;
// getters and setters
}Register the auto‑configuration class in src/main/resources/META-INF/spring.factories:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xx.xx.xx.autoconfigure.xxxAutoConfigurationSpring Boot’s auto‑configuration works by reading META-INF/spring.factories on startup, loading all classes listed under EnableAutoConfiguration, and processing their @Bean methods. Configuration properties are bound to POJOs annotated with @ConfigurationProperties, allowing defaults to be overridden in application.yml or application.properties.
In summary, a Spring Boot starter aggregates dependencies, provides an auto‑configuration class and a properties holder, and is registered via spring.factories. Understanding these components enables developers to quickly create reusable starters for any scenario.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.
