Spring Boot Auto Configuration

Auto configuration is the feature that makes Spring Boot "just work." When you add a library to your project, Spring Boot detects it and sets it up automatically — no extra code required from you.

The Library-in-a-Box Analogy

Imagine buying a Wi-Fi router. Traditional Spring is like a router that ships with no default settings — you configure every IP address, channel, and security setting manually. Spring Boot is like a router that comes pre-configured with sensible defaults. It works out of the box. You only change settings when you need something specific.

How Auto Configuration Works

App starts
    │
    ▼
Spring Boot reads your classpath
(What JARs are present?)
    │
    ▼
Checks conditions:
  Is H2 database JAR present? → Configure in-memory DB
  Is Tomcat JAR present?      → Start Tomcat on port 8080
  Is Jackson JAR present?     → Enable JSON conversion
    │
    ▼
Applies matching configurations automatically
    │
    ▼
Your app is ready

The @Conditional Mechanism

Every auto-configuration class uses conditions to decide whether to activate. Spring Boot checks these conditions at startup and skips configurations that do not apply.

Condition Annotation             When It Activates
──────────────────────────       ──────────────────────────────────
@ConditionalOnClass              A specific class exists in the classpath
@ConditionalOnMissingBean        No bean of that type has been defined yet
@ConditionalOnProperty           A specific property is set in config
@ConditionalOnWebApplication     The app is a web application

Practical Example

The DataSourceAutoConfiguration class runs only when:

  • A JDBC driver is on the classpath (@ConditionalOnClass)
  • No custom DataSource bean already exists (@ConditionalOnMissingBean)

If you define your own DataSource bean, Spring Boot detects it and skips its own configuration entirely. Your custom setup always wins.

Seeing What Gets Auto-Configured

Add this to application.properties to see every auto-configuration decision at startup:

debug=true

The console prints a CONDITIONS EVALUATION REPORT showing:

  • Positive matches — configurations that activated
  • Negative matches — configurations that were skipped and why

Overriding Auto Configuration

You take control by defining your own beans or setting properties. Spring Boot always backs off when it detects your custom setup.

Override via Properties

# Change the default Tomcat port
server.port=9090

# Use HikariCP pool size different from default
spring.datasource.hikari.maximum-pool-size=20

Override via Custom Bean

@Configuration
public class MyConfig {

    @Bean
    public DataSource dataSource() {
        // Your custom DataSource — Spring Boot backs off
        return new HikariDataSource(myCustomConfig());
    }

}

Auto Configuration Is Not Magic

What you think happens:       What actually happens:
────────────────────────      ──────────────────────────────────────
"Spring Boot guesses"    →    Spring Boot reads classpath + conditions
"It's hidden"            →    It's in spring-boot-autoconfigure.jar
"Can't be changed"       →    Your beans and properties override it

All auto-configuration classes are open source and readable. You can inspect the spring-boot-autoconfigure source code on GitHub to see exactly what each one does.

Disabling Specific Auto Configurations

Exclude an auto-configuration class if you want Spring Boot to skip it entirely:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DemoApplication { ... }

This tells Spring Boot not to set up a database connection — useful when your app does not use a database.

Summary

  • Auto configuration detects what JARs are present and configures them automatically
  • It uses @Conditional annotations to decide what to activate
  • Your custom beans always override auto-configured defaults
  • Set debug=true in properties to see every configuration decision
  • Use exclude in @SpringBootApplication to disable specific configurations

Leave a Comment

Your email address will not be published. Required fields are marked *