How Boot's Auto-Configuration Builds on Spring
How Boot's Auto-Configuration Builds on Spring
Spring Boot feels like magic until you realize it is built almost entirely from core Spring features. Auto-configuration is a library of conditional configuration classes that only act when certain conditions hold. You can lift the hood and see the same annotations you already know.
Conditions gate everything
Each auto-configuration class checks conditions with annotations like @ConditionalOnClass, @ConditionalOnBean, and @ConditionalOnMissingBean. If the web starter is present, Tomcat and MVC classes exist, so web auto-configuration turns on. If you define your own data source, @ConditionalOnMissingBean backs off and uses yours.
@Configuration
@ConditionalOnClass(DataSource.class)
public class DataSourceAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.build();
}
}
It is still Spring
Auto-configuration classes are plain @Configuration classes. Their @Bean methods return beans into the same application context you would manage by hand in the core framework. Boot just wrote the conditions for you, based on your classpath and properties.
Override by adding your own bean
Because defaults are conditional on missing beans, your own bean takes precedence. Define a DataSource, an ObjectMapper, or a RouterFunction, and Boot backs off. This is the override mechanism at the heart of Boot's flexibility.
Under the hood in Boot
Boot's annotations on your main class - @SpringBootApplication - combine @Configuration, @EnableAutoConfiguration, and component scanning. So a Boot application is fundamentally a Spring context with extra conventions and sensible defaults layered on top.
Key Points
- Auto-configuration is a library of conditional configuration classes.
- Conditions check the classpath, existing beans, and properties.
- Your own beans override Boot's defaults via
@ConditionalOnMissingBean. @SpringBootApplicationbundles configuration, auto-configuration, and scanning.- Boot is core Spring with conventions and defaults applied.