Auto-configuration & Starters
You added spring-boot-starter-web and suddenly had Tomcat, Jackson and Spring MVC configured. You added the H2 driver and a DataSource appeared. That is auto-configuration: Spring Boot looks at what is on the classpath and which beans and properties you defined, and registers sensible beans for everything you did not configure yourself.
Starters
A starter is a dependency with no code of its own — just a curated list of other dependencies.
| Starter | Brings in |
|---|---|
spring-boot-starter-web |
Spring MVC, embedded Tomcat, Jackson |
spring-boot-starter-validation |
Hibernate Validator (Jakarta Bean Validation) |
spring-boot-starter-data-jpa |
Hibernate ORM, Spring Data JPA, HikariCP connection pool |
spring-boot-starter-security |
Spring Security |
spring-boot-starter-actuator |
health, metrics and info endpoints |
spring-boot-starter-test |
JUnit 5, AssertJ, Mockito, Spring Test, JSONPath |
./mvnw dependency:tree -Dincludes=org.springframework.boot | head -8[INFO] com.example:tasks:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:3.5.6:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:3.5.6:compile
[INFO] | | +- org.springframework.boot:spring-boot:jar:3.5.6:compile
[INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:3.5.6:compile
[INFO] | | \- org.springframework.boot:spring-boot-starter-logging:jar:3.5.6:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:3.5.6:compile
[INFO] | \- org.springframework.boot:spring-boot-starter-tomcat:jar:3.5.6:compile
How auto-configuration decides
spring-boot-autoconfigure.jar contains ~150 ordinary @AutoConfiguration classes listed in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. Each one is guarded by conditions:
@AutoConfiguration
@ConditionalOnClass(ObjectMapper.class) // only if Jackson is on the classpath
public class JacksonAutoConfiguration {
@Bean
@ConditionalOnMissingBean // only if you did not define one
ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
return builder.createXmlMapper(false).build();
}
}(Simplified from the real class.) The most important conditions:
| Condition | Matches when |
|---|---|
@ConditionalOnClass |
a class is on the classpath |
@ConditionalOnMissingClass |
a class is not on the classpath |
@ConditionalOnBean / @ConditionalOnMissingBean |
a bean of that type exists / does not exist |
@ConditionalOnProperty |
a property has a given value |
@ConditionalOnWebApplication |
the app is a web application |
The golden rule is @ConditionalOnMissingBean: your beans always win. Define your own ObjectMapper and Boot's backs off.
Simulating the decision
This program evaluates conditions the way Boot does, for a fake classpath and a set of user-defined beans:
import java.util.*;
import java.util.function.Predicate;
record Env(Set<String> classpath, Set<String> userBeans, Map<String, String> props) {}
record AutoConfig(String name, String bean, Predicate<Env> condition, String reason) {}
class Main {
public static void main(String[] args) {
Env env = new Env(
Set.of("ObjectMapper", "Tomcat", "DispatcherServlet"),
Set.of("objectMapper"),
Map.of("spring.main.banner-mode", "off"));
List<AutoConfig> candidates = List.of(
new AutoConfig("JacksonAutoConfiguration", "objectMapper",
e -> e.classpath().contains("ObjectMapper") && !e.userBeans().contains("objectMapper"),
"@ConditionalOnMissingBean"),
new AutoConfig("TomcatWebServerAutoConfiguration", "tomcatFactory",
e -> e.classpath().contains("Tomcat"), "@ConditionalOnClass(Tomcat)"),
new AutoConfig("DataSourceAutoConfiguration", "dataSource",
e -> e.classpath().contains("HikariDataSource"), "@ConditionalOnClass(HikariDataSource)"),
new AutoConfig("SecurityAutoConfiguration", "securityFilterChain",
e -> e.classpath().contains("SecurityFilterChain"), "@ConditionalOnClass(SecurityFilterChain)"));
for (AutoConfig c : candidates) {
boolean match = c.condition().test(env);
System.out.printf("%-34s %-9s %s%n", c.name(), match ? "MATCHED" : "skipped", c.reason());
}
}
}JacksonAutoConfiguration skipped @ConditionalOnMissingBean
TomcatWebServerAutoConfiguration MATCHED @ConditionalOnClass(Tomcat)
DataSourceAutoConfiguration skipped @ConditionalOnClass(HikariDataSource)
SecurityAutoConfiguration skipped @ConditionalOnClass(SecurityFilterChain)
Jackson's auto-configured mapper is skipped because the user already defined objectMapper; no database or security classes are present, so those configurations stay out of the way.
The conditions evaluation report
To see the real decisions, start the app with --debug (or set debug=true):
./mvnw spring-boot:run -Dspring-boot.run.arguments=--debug | grep -A3 "DataSourceAutoConfiguration" DataSourceAutoConfiguration matched:
- @ConditionalOnClass found required classes 'javax.sql.DataSource', 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType' (OnClassCondition)
- @ConditionalOnMissingBean (types: io.r2dbc.spi.ConnectionFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)
The report has sections Positive matches, Negative matches, Exclusions and Unconditional classes. It is the first place to look when Boot creates (or fails to create) a bean you expected.
Turning auto-configuration off
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class TasksApplication { ... }or in application.properties:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationMost of the time you don't exclude anything — you either define your own bean or change a property such as server.port or spring.jackson.property-naming-strategy, and the auto-configured bean adapts.
Tip: The reference documentation's Common Application Properties appendix lists every property that auto-configuration reads. Your IDE also autocompletes them in
application.properties.
Test yourself
What does @ConditionalOnMissingBean on an auto-configured bean achieve?
- It forces Boot's bean to replace yours
- Boot's bean is only created if you have not defined one yourself
- It removes the bean at shutdown
- It makes the bean lazy
What is a Spring Boot starter?
- A main class
- A dependency that aggregates a curated set of other dependencies
- A shell script
- A Maven plugin goal
How can you see why an auto-configuration did or did not apply?
- Run with
--debugto print the conditions evaluation report - Read the JAR manifest
- Use
jps - It is not possible