Implementation Principles of Spring Boot Auto‑Configuration
This article explains how Spring Boot’s auto‑configuration works by describing the role of @SpringBootApplication, the conditional annotations that drive bean creation, the use of META‑INF/spring.factories, and provides a step‑by‑step example of building a custom auto‑configuration module with full code samples.
Spring Boot’s core feature is automatic configuration, which creates beans based on conditional checks. The auto‑configuration classes are packaged in spring-boot-autoconfigure-2.0.3.RELEASE.jar and are activated through the @EnableAutoConfiguration annotation.
Configuration Properties
Typical property binding uses @ConfigurationProperties(prefix="yourPrefix") . An example shows a @RestController that reads a test.msg property from application.yml and returns it via a REST endpoint.
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.boot.context.properties.ConfigurationProperties;
@RestController
@ConfigurationProperties(prefix = "test")
public class Demo {
private String msg = "default";
public String getMsg() { return msg; }
public void setMsg(String msg) { this.msg = msg; }
@RequestMapping("/msg")
public Object index() { return this.msg; }
}SpringBootApplication Annotation
The @SpringBootApplication annotation combines @SpringBootConfiguration , @EnableAutoConfiguration , and @ComponentScan . The most critical part is @EnableAutoConfiguration , which imports EnableAutoConfigurationImportSelector to load all candidate configuration classes listed in META-INF/spring.factories .
Conditional Annotations
Spring Boot uses a variety of @Conditional… annotations (e.g., @ConditionalOnProperty , @ConditionalOnClass , @ConditionalOnMissingBean ) to decide whether a bean should be instantiated. Only when all conditions evaluate to true is the bean created.
Example: SpringApplicationAdminJmxAutoConfiguration
The class is annotated with @Configuration , @AutoConfigureAfter , and @ConditionalOnProperty to ensure it is only loaded when the property spring.application.admin.enabled is true.
Creating a Custom Auto‑Configuration
A custom module consists of a service class ( BambooServer ), a properties class ( BambooServerProperties ) annotated with @ConfigurationProperties(prefix="bamboo") , and an auto‑configuration class that uses @ConditionalOnClass and @ConditionalOnProperty to register a BambooServer bean when appropriate.
public class BambooServer {
private String name;
public String sayServerName() { return "I'm " + name + "! "; }
// getters and setters omitted
} @ConfigurationProperties(prefix = "bamboo")
public class BambooServerProperties {
private String name = "bamboo_server0";
// getters and setters omitted
} @Configuration
@EnableConfigurationProperties(BambooServerProperties.class)
@ConditionalOnClass(BambooServer.class)
@ConditionalOnProperty(prefix = "bamboo", value = "enabled", matchIfMissing = true)
public class BmbooServiceAutoConfiguration {
@Autowired
private BambooServerProperties props;
@Bean(name = "bambooServer")
@ConditionalOnMissingBean(BambooServer.class)
public BambooServer bambooServer() {
BambooServer server = new BambooServer();
server.setName(props.getName());
return server;
}
}The auto‑configuration class is listed in src/main/resources/META-INF/spring.factories so that SpringFactoriesLoader can discover it.
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.bamboo.common.autoconfigure.bamboo.BmbooServiceAutoConfigurationWhen the application runs, the bean is created automatically; changing the property bamboo.name in application.yml updates the service output without additional code.
Summary Diagram
The article concludes with a diagram illustrating the relationship between Spring Boot’s auto‑configuration components and shows that many starter modules (e.g., mybatis-spring-boot-starter , spring-boot-starter-web ) also rely on spring.factories to register their auto‑configuration classes.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.