All articles
Spring BootExam Tips

Spring Boot Auto-configuration Explained: How @EnableAutoConfiguration Works

March 3, 20265 min read

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:

AnnotationActivates when…
@ConditionalOnClassA specific class is on the classpath
@ConditionalOnMissingClassA class is NOT on the classpath
@ConditionalOnBeanA specific bean already exists
@ConditionalOnMissingBeanA bean does NOT already exist
@ConditionalOnPropertyA property is set (with optional value)
@ConditionalOnResourceA resource file exists on the classpath
@ConditionalOnWebApplicationThe context is a web application
@ConditionalOnNotWebApplicationThe 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 class
  • before — 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:

  1. What triggers auto-configuration?@EnableAutoConfiguration (part of @SpringBootApplication)
  2. How does Spring Boot know which auto-configs to load?AutoConfiguration.imports file in each starter JAR
  3. How do you override auto-configured beans? → Define your own @Bean@ConditionalOnMissingBean causes auto-config to back off
  4. How do you disable auto-configuration?exclude attribute on @SpringBootApplication or spring.autoconfigure.exclude property
  5. What is @ConditionalOnClass? → Activates a bean only if a class is present on the classpath

Summary

ConceptKey Point
@EnableAutoConfigurationEntry point — loads all auto-config candidates
AutoConfiguration.importsLists candidate classes per JAR (Boot 3.x)
@ConditionalOnClassMost common condition — classpath-based activation
@ConditionalOnMissingBeanYour bean takes priority — auto-config backs off
exclude attributeOpt out of specific auto-configurations
/actuator/conditionsDiagnose 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.

Practice This Topic

Reinforce what you've learned with free practice questions and detailed explanations.