What Is Auto-configuration?
Auto-configuration is the feature that makes Spring Boot opinionated. Instead of forcing you to wire every bean manually, Spring Boot inspects your classpath, existing beans, and properties — then configures sensible defaults automatically.
This is the core of the "convention over configuration" philosophy and one of the highest-weighted topics on the 2V0-72.22 exam (Spring Boot = 28% of all questions).
The Entry Point: @SpringBootApplication
Every Spring Boot application starts with @SpringBootApplication, which is a composed annotation combining three things:
@SpringBootConfiguration // = @Configuration
@EnableAutoConfiguration // triggers auto-config
@ComponentScan // scans the current package
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
The key annotation is @EnableAutoConfiguration.
How @EnableAutoConfiguration Works
@EnableAutoConfiguration tells Spring Boot to load auto-configuration classes from the classpath. The mechanism has evolved across versions:
Spring Boot 2.x — spring.factories
Spring Boot 2.x used a file at:
META-INF/spring.factories
Inside each starter JAR, this file lists auto-configuration classes:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Spring Boot read this file at startup and registered all listed classes as candidates.
Spring Boot 3.x — AutoConfiguration.imports
Spring Boot 3.x (covered by the current exam) replaced spring.factories with:
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
Each line is one auto-configuration class name. This is more explicit and faster to process.
The @Conditional System
Auto-configuration classes are NOT applied blindly. They use @Conditional annotations to decide whether to activate:
| Annotation | Activates when… |
|---|---|
@ConditionalOnClass | A specific class is on the classpath |
@ConditionalOnMissingClass | A class is NOT on the classpath |
@ConditionalOnBean | A specific bean already exists |
@ConditionalOnMissingBean | A bean does NOT already exist |
@ConditionalOnProperty | A property is set (with optional value) |
@ConditionalOnResource | A resource file exists on the classpath |
@ConditionalOnWebApplication | The context is a web application |
@ConditionalOnNotWebApplication | The context is NOT a web application |
Example: DataSourceAutoConfiguration
@AutoConfiguration
@ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class })
@ConditionalOnMissingBean(type = "io.r2dbc.spi.ConnectionFactory")
@EnableConfigurationProperties(DataSourceProperties.class)
public class DataSourceAutoConfiguration {
// Creates DataSource bean only if:
// 1. DataSource class is on classpath (e.g., H2, PostgreSQL driver)
// 2. No R2DBC connection factory exists
}
If you add spring-boot-starter-data-jpa to your project, DataSource.class appears on the classpath → the condition passes → Spring Boot auto-configures a DataSource for you.
Override Auto-configuration with Your Own Beans
The most important @Conditional for developers is @ConditionalOnMissingBean. This means:
"Apply this auto-configuration ONLY if the user hasn't already defined their own bean."
This is how you override defaults:
@Configuration
public class MyDataSourceConfig {
@Bean
public DataSource dataSource() {
// Your custom DataSource — Spring Boot's auto-config steps back
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:postgresql://localhost/mydb");
return ds;
}
}
Because @ConditionalOnMissingBean(DataSource.class) sees your bean, the auto-configuration is skipped entirely.
Excluding Auto-configuration
Sometimes you want to disable a specific auto-configuration class:
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
SecurityAutoConfiguration.class
})
public class MyApp { ... }
Or via application.properties:
spring.autoconfigure.exclude=\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
This is a common exam question: how do you disable auto-configuration for a specific module?
Auto-configuration Order
Auto-configuration classes can declare ordering using:
@AutoConfiguration(after = DataSourceAutoConfiguration.class)
public class JpaRepositoriesAutoConfiguration { ... }
after— run this after the specified classbefore— run this before the specified class@AutoConfigureOrder— numeric ordering (lower = earlier)
Debugging Auto-configuration
Spring Boot provides a built-in report. Add to application.properties:
logging.level.org.springframework.boot.autoconfigure=DEBUG
Or use the Actuator endpoint (if spring-boot-starter-actuator is on classpath):
GET /actuator/conditions
The response shows:
- Positive matches — auto-configs that were applied
- Negative matches — auto-configs that were skipped (and why)
- Unconditional classes — always applied
This is invaluable for understanding why a bean is or isn't being created.
Exam Tips
The 2V0-72.22 exam frequently tests these scenarios:
- What triggers auto-configuration? →
@EnableAutoConfiguration(part of@SpringBootApplication) - How does Spring Boot know which auto-configs to load? →
AutoConfiguration.importsfile in each starter JAR - How do you override auto-configured beans? → Define your own
@Bean—@ConditionalOnMissingBeancauses auto-config to back off - How do you disable auto-configuration? →
excludeattribute on@SpringBootApplicationorspring.autoconfigure.excludeproperty - What is
@ConditionalOnClass? → Activates a bean only if a class is present on the classpath
Summary
| Concept | Key Point |
|---|---|
@EnableAutoConfiguration | Entry point — loads all auto-config candidates |
AutoConfiguration.imports | Lists candidate classes per JAR (Boot 3.x) |
@ConditionalOnClass | Most common condition — classpath-based activation |
@ConditionalOnMissingBean | Your bean takes priority — auto-config backs off |
exclude attribute | Opt out of specific auto-configurations |
/actuator/conditions | Diagnose what was auto-configured and why |
Understanding auto-configuration is the key to understanding Spring Boot. Once you see how conditions work, the whole "magic" becomes predictable and controllable.