Definition
- @EnableAutoConfiguration is an annotation included in @SpringBootApplication that triggers automatic bean configuration based on classpath contents and application properties.
- Auto-configuration class is a @Configuration class listed in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports that Spring Boot loads automatically.
- @ConditionalOnClass creates beans only if a specific class is on the classpath; for example, DataSourceAutoConfiguration activates only if javax.sql.DataSource is available.
- @ConditionalOnMissingBean is the back-off mechanism: the auto-configuration skips a bean if you have already defined one of the same type.
- @ConditionalOnProperty creates beans only if a specific property is set in application.properties or application.yml.
- CONDITIONS EVALUATION REPORT is the debug output printed at startup when debug=true, showing which auto-configurations matched and which did not.
Think of National Bank of India setting up a new branch. The headquarters (Spring Boot) sends a setup team that checks the building before configuring anything. If the building has a vault room (classpath has a database driver), the team installs the vault management system (DataSource auto-configuration). If the branch already has its own custom vault system installed (you defined your own DataSource bean), the team backs off and skips the default setup. If the building has no vault room, the team skips vault configuration entirely. Every decision is conditional on what is already present.
Understanding the Concepts
What is @EnableAutoConfiguration?
@EnableAutoConfiguration is an annotation that tells Spring Boot: "look at the jars on my classpath and automatically configure beans that I am likely to need." If spring-boot-starter-web is on the classpath, it configures an embedded Tomcat server, a DispatcherServlet, and Jackson for JSON. If spring-boot-starter-data-jpa is present, it configures a DataSource, EntityManagerFactory, and transaction manager.
You almost never use @EnableAutoConfiguration directly because @SpringBootApplication already includes it.
package com.bank;
public class AutoConfigExplained {
public static void main(String[] args) {
System.out.println("@SpringBootApplication includes:");
System.out.println(" 1. @SpringBootConfiguration (= @Configuration)");
System.out.println(" 2. @EnableAutoConfiguration (auto-configure beans)");
System.out.println(" 3. @ComponentScan (scan for @Component classes)");
System.out.println();
System.out.println("What @EnableAutoConfiguration does:");
System.out.println(" 1. Loads AutoConfiguration.imports file");
System.out.println(" 2. For each auto-config class, evaluates @Conditional");
System.out.println(" 3. If conditions match, creates the beans");
System.out.println(" 4. If you defined your own bean, auto-config backs off");
}
}
Expected output:
@SpringBootApplication includes:
1. @SpringBootConfiguration (= @Configuration)
2. @EnableAutoConfiguration (auto-configure beans)
3. @ComponentScan (scan for @Component classes)
What @EnableAutoConfiguration does:
1. Loads AutoConfiguration.imports file
2. For each auto-config class, evaluates @Conditional
3. If conditions match, creates the beans
4. If you defined your own bean, auto-config backs off
How does auto-configuration work internally?
When @EnableAutoConfiguration is processed, Spring Boot uses AutoConfigurationImportSelector to load class names from META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. In Spring Boot 3.4, there are over 140 auto-configuration classes.
For each class, Spring evaluates the @Conditional annotations. If all conditions pass, the @Configuration class is processed and its @Bean methods create beans. If any condition fails, the entire class is skipped.
package com.bank;
public class AutoConfigInternals {
public static void main(String[] args) {
System.out.println("Auto-configuration loading sequence:");
System.out.println(" 1. @EnableAutoConfiguration triggers AutoConfigurationImportSelector");
System.out.println(" 2. Selector reads AutoConfiguration.imports from all jars");
System.out.println(" 3. Found 140+ auto-configuration class names");
System.out.println(" 4. For each class:");
System.out.println(" a. Check @ConditionalOnClass: is the required class on classpath?");
System.out.println(" b. Check @ConditionalOnProperty: is the property set?");
System.out.println(" c. Check @ConditionalOnMissingBean: has the user defined their own?");
System.out.println(" d. If ALL conditions pass, process @Bean methods");
System.out.println(" e. If ANY condition fails, skip this auto-config entirely");
System.out.println();
System.out.println("Example: DataSourceAutoConfiguration");
System.out.println(" @ConditionalOnClass(DataSource.class) -> is JDBC driver on classpath?");
System.out.println(" @ConditionalOnMissingBean(DataSource.class) -> has user defined DataSource?");
System.out.println(" Both pass -> create HikariDataSource bean");
}
}
Expected output:
Auto-configuration loading sequence:
1. @EnableAutoConfiguration triggers AutoConfigurationImportSelector
2. Selector reads AutoConfiguration.imports from all jars
3. Found 140+ auto-configuration class names
4. For each class:
a. Check @ConditionalOnClass: is the required class on classpath?
b. Check @ConditionalOnProperty: is the property set?
c. Check @ConditionalOnMissingBean: has the user defined their own?
d. If ALL conditions pass, process @Bean methods
e. If ANY condition fails, skip this auto-config entirely
Example: DataSourceAutoConfiguration
@ConditionalOnClass(DataSource.class) -> is JDBC driver on classpath?
@ConditionalOnMissingBean(DataSource.class) -> has user defined DataSource?
Both pass -> create HikariDataSource bean
What are the key @Conditional annotations?
| Annotation | Condition | Example |
|---|---|---|
| @ConditionalOnClass | Class exists on classpath | @ConditionalOnClass(DataSource.class) |
| @ConditionalOnMissingClass | Class does NOT exist on classpath | @ConditionalOnMissingClass("com.oracle.OracleDriver") |
| @ConditionalOnBean | A bean of the given type exists | @ConditionalOnBean(DataSource.class) |
| @ConditionalOnMissingBean | No bean of the given type exists | @ConditionalOnMissingBean(DataSource.class) |
| @ConditionalOnProperty | Property has a specific value | @ConditionalOnProperty(name="audit.enabled", havingValue="true") |
| @ConditionalOnWebApplication | App is a web application | @ConditionalOnWebApplication(type=SERVLET) |
| @ConditionalOnResource | A resource exists on classpath | @ConditionalOnResource(resources="classpath:schema.sql") |
package com.bank.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AuditConfig {
@Bean
@ConditionalOnProperty(name = "bank.audit.enabled", havingValue = "true", matchIfMissing = false)
String auditLogger() {
System.out.println("Audit logger created (bank.audit.enabled=true)");
return "AuditLogger-Active";
}
}
Expected output (with bank.audit.enabled=true in application.properties):
Audit logger created (bank.audit.enabled=true)
Expected output (without the property or bank.audit.enabled=false):
(nothing printed, bean not created)
What is a complete banking project with custom auto-configuration?
This project creates a custom auto-configuration module for a banking audit feature. The audit module configures itself automatically if the AuditService class is on the classpath and the user has not defined their own AuditService bean.
Project structure
bank-autoconfig-demo/
├── pom.xml
└── src/
├── main/
│ ├── java/
│ │ └── com/
│ │ └── bank/
│ │ ├── BankAutoConfigApp.java
│ │ ├── audit/
│ │ │ ├── AuditService.java
│ │ │ └── AuditAutoConfiguration.java
│ │ └── service/
│ │ └── PaymentService.java
│ └── resources/
│ └── application.properties
└── test/
└── java/
└── com/
└── bank/
└── audit/
└── AuditAutoConfigurationTest.java
pom.xml
<?xml version="1.0" encoding="UTF-8"?syn-kw">>
syn-kw"><project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"syn-kw">>
syn-kw"><modelVersionsyn-kw">>4.0.0syn-kw"></modelVersionsyn-kw">>
syn-kw"><parentsyn-kw">>
syn-kw"><groupIdsyn-kw">>org.springframework.bootsyn-kw"></groupIdsyn-kw">>
syn-kw"><artifactIdsyn-kw">>spring-boot-starter-parentsyn-kw"></artifactIdsyn-kw">>
syn-kw"><versionsyn-kw">>3.4.7syn-kw"></versionsyn-kw">>
syn-kw"></parentsyn-kw">>
syn-kw"><groupIdsyn-kw">>com.banksyn-kw"></groupIdsyn-kw">>
syn-kw"><artifactIdsyn-kw">>bank-autoconfig-demosyn-kw"></artifactIdsyn-kw">>
syn-kw"><versionsyn-kw">>1.0.0syn-kw"></versionsyn-kw">>
syn-kw"><propertiessyn-kw">>
syn-kw"><java.versionsyn-kw">>25syn-kw"></java.versionsyn-kw">>
syn-kw"></propertiessyn-kw">>
syn-kw"><dependenciessyn-kw">>
syn-kw"><dependencysyn-kw">>
syn-kw"><groupIdsyn-kw">>org.springframework.bootsyn-kw"></groupIdsyn-kw">>
syn-kw"><artifactIdsyn-kw">>spring-boot-startersyn-kw"></artifactIdsyn-kw">>
syn-kw"></dependencysyn-kw">>
syn-kw"><dependencysyn-kw">>
syn-kw"><groupIdsyn-kw">>org.springframework.bootsyn-kw"></groupIdsyn-kw">>
syn-kw"><artifactIdsyn-kw">>spring-boot-starter-testsyn-kw"></artifactIdsyn-kw">>
syn-kw"><scopesyn-kw">>testsyn-kw"></scopesyn-kw">>
syn-kw"></dependencysyn-kw">>
syn-kw"></dependenciessyn-kw">>
syn-kw"></projectsyn-kw">>
AuditService.java
package com.bank.audit;
public class AuditService {
private final String auditDestination;
public AuditService(String auditDestination) {
this.auditDestination = auditDestination;
}
public String audit(String accountId, String action) {
return "AUDIT [" + auditDestination + "]: " + accountId + " - " + action;
}
}
AuditAutoConfiguration.java
package com.bank.audit;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnClass(AuditService.class)
@ConditionalOnProperty(name = "bank.audit.enabled", havingValue = "true", matchIfMissing = false)
public class AuditAutoConfiguration {
@Bean
@ConditionalOnMissingBean(AuditService.class)
AuditService auditService() {
System.out.println("Auto-configured AuditService (default: file-based)");
return new AuditService("file:/var/log/bank-audit.log");
}
}
PaymentService.java
package com.bank.service;
import com.bank.audit.AuditService;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
@Service
public class PaymentService {
private final AuditService auditService;
public PaymentService(AuditService auditService) {
this.auditService = auditService;
}
public String processPayment(String accountId, BigDecimal amount) {
String auditResult = auditService.audit(accountId, "PAYMENT " + amount + " INR");
System.out.println(auditResult);
return "Payment processed for " + accountId;
}
}
application.properties
bank.audit.enabled=true
BankAutoConfigApp.java
package com.bank;
import com.bank.service.PaymentService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.math.BigDecimal;
@SpringBootApplication
public class BankAutoConfigApp {
public static void main(String[] args) {
SpringApplication.run(BankAutoConfigApp.class, args);
}
@Bean
CommandLineRunner demo(PaymentService paymentService) {
return args -> {
System.out.println(paymentService.processPayment("SAV2001", new BigDecimal("50000.00")));
System.out.println(paymentService.processPayment("CUR3001", new BigDecimal("200000.00")));
};
}
}
AuditAutoConfigurationTest.java
package com.bank.audit;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest(properties = "bank.audit.enabled=true")
class AuditAutoConfigurationTest {
@Autowired
private ApplicationContext context;
@Test
void auditServiceIsAutoConfigured() {
assertTrue(context.containsBean("auditService"));
var service = context.getBean(AuditService.class);
String result = service.audit("SAV2001", "TEST");
System.out.println("Test passed: " + result);
}
}
Expected output (application run):
Auto-configured AuditService (default: file-based)
AUDIT [file:/var/log/bank-audit.log]: SAV2001 - PAYMENT 50000.00 INR
Payment processed for SAV2001
AUDIT [file:/var/log/bank-audit.log]: CUR3001 - PAYMENT 200000.00 INR
Payment processed for CUR3001
How to run
cd bank-autoconfig-demo
mvn clean package -DskipTests
java -jar target/bank-autoconfig-demo-1.0.0.jar
# Run tests
mvn test
# Debug auto-configuration decisions
java -jar target/bank-autoconfig-demo-1.0.0.jar --debug
How do you debug auto-configuration decisions?
Set debug=true in application.properties or pass --debug on the command line. Spring Boot prints a CONDITIONS EVALUATION REPORT showing which auto-configurations matched and which did not.
package com.bank;
public class DebugAutoConfig {
public static void main(String[] args) {
System.out.println("To see auto-configuration decisions:");
System.out.println(" Option 1: application.properties -> debug=true");
System.out.println(" Option 2: command line -> java -jar app.jar --debug");
System.out.println();
System.out.println("CONDITIONS EVALUATION REPORT (sample):");
System.out.println("============================");
System.out.println("Positive matches:");
System.out.println(" AuditAutoConfiguration matched:");
System.out.println(" - @ConditionalOnClass found AuditService");
System.out.println(" - @ConditionalOnProperty bank.audit.enabled=true");
System.out.println(" - @ConditionalOnMissingBean no user-defined AuditService");
System.out.println();
System.out.println("Negative matches:");
System.out.println(" DataSourceAutoConfiguration:");
System.out.println(" - @ConditionalOnClass did not find javax.sql.DataSource");
}
}
Expected output:
To see auto-configuration decisions:
Option 1: application.properties -> debug=true
Option 2: command line -> java -jar app.jar --debug
CONDITIONS EVALUATION REPORT (sample):
============================
Positive matches:
AuditAutoConfiguration matched:
- @ConditionalOnClass found AuditService
- @ConditionalOnProperty bank.audit.enabled=true
- @ConditionalOnMissingBean no user-defined AuditService
Negative matches:
DataSourceAutoConfiguration:
- @ConditionalOnClass did not find javax.sql.DataSource
What are common mistakes with auto-configuration?
Wrong: not understanding @ConditionalOnMissingBean back-off
package com.bank.config;
import com.bank.audit.AuditService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomAuditConfig {
@Bean
AuditService auditService() {
// This custom bean is created
return new AuditService("kafka://audit-topic");
}
@Bean
AuditService anotherAuditService() {
// WRONG: two beans of the same type!
// The auto-config sees auditService exists, backs off.
// But now you have a duplicate.
return new AuditService("sqs://audit-queue");
}
}
Problem: two AuditService beans defined.
Spring throws NoUniqueBeanDefinitionException when something injects AuditService.
The auto-configured one backed off (correct), but the user created two (wrong).
Correct: define one custom bean
package com.bank.config;
import com.bank.audit.AuditService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomAuditConfig {
@Bean
AuditService auditService() {
// This replaces the auto-configured one (which backs off)
System.out.println("Custom AuditService created (Kafka-based)");
return new AuditService("kafka://audit-topic");
}
}
Correct: one custom AuditService bean.
Auto-configured AuditService backs off because @ConditionalOnMissingBean finds yours.
All injections receive the Kafka-based audit service.
What should a senior or principal engineer know?
- AutoConfiguration.imports file: in Spring Boot 3.x, auto-configuration classes are registered in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. The old spring.factories file is deprecated for auto-configuration.
- @AutoConfiguration annotation: replaces @Configuration on auto-config classes. It supports @AutoConfigureBefore and @AutoConfigureAfter for ordering.
- Condition evaluation order: class-level conditions (@ConditionalOnClass) are evaluated first. If the class-level condition fails, method-level conditions are not checked. This avoids loading classes that might not be on the classpath.
- Writing your own starter: create a library with an auto-configuration class, register it in AutoConfiguration.imports, and publish it. Users add the dependency and beans appear automatically.
- Startup performance: each auto-configuration class that passes its conditions takes time to process. Spring Boot 3.x is faster because it uses ahead-of-time condition evaluation in native images.
package com.bank.core;
public class SeniorAutoConfigKnowledge {
public static void main(String[] args) {
System.out.println("Senior/Principal Auto-Configuration Checklist:");
System.out.println(" 1. AutoConfiguration.imports replaces spring.factories in Boot 3.x");
System.out.println(" 2. @AutoConfiguration replaces @Configuration for auto-config classes");
System.out.println(" 3. Class-level @Conditional evaluated before method-level");
System.out.println(" 4. @ConditionalOnMissingBean is the back-off mechanism");
System.out.println(" 5. debug=true prints CONDITIONS EVALUATION REPORT");
System.out.println(" 6. Custom starters: auto-config class + AutoConfiguration.imports");
System.out.println(" 7. Native images use AOT for condition evaluation at compile time");
}
}
Expected output:
Senior/Principal Auto-Configuration Checklist:
1. AutoConfiguration.imports replaces spring.factories in Boot 3.x
2. @AutoConfiguration replaces @Configuration for auto-config classes
3. Class-level @Conditional evaluated before method-level
4. @ConditionalOnMissingBean is the back-off mechanism
5. debug=true prints CONDITIONS EVALUATION REPORT
6. Custom starters: auto-config class + AutoConfiguration.imports
7. Native images use AOT for condition evaluation at compile time
What are common interview follow-up questions?
- How many auto-configuration classes does Spring Boot 3.4 have? (Over 140. You can count them by looking at the AutoConfiguration.imports file in spring-boot-autoconfigure.jar.)
- What is the difference between @ConditionalOnBean and @ConditionalOnMissingBean? (@ConditionalOnBean creates the bean only if a specific bean already exists. @ConditionalOnMissingBean creates the bean only if no bean of that type exists.)
- Can you write your own @Conditional annotation? (Yes. Implement the Condition interface with a matches() method. The method receives ConditionContext and AnnotatedTypeMetadata.)
- What is the difference between spring.factories and AutoConfiguration.imports? (spring.factories is the old format from Spring Boot 2.x. AutoConfiguration.imports is the new format in Spring Boot 3.x. Both list auto-configuration classes, but the new format only supports auto-configuration, not other factory types.)
- Does auto-configuration run before or after component scanning? (After. Component scanning discovers your beans first, then auto-configuration runs and backs off where your beans exist.)
Visual Overview
@EnableAutoConfiguration Flow
================================
┌──────────────────────────┐
│ @SpringBootApplication │
│ includes: │
│ @EnableAutoConfiguration │
└────────────┬─────────────┘
│
▼
┌──────────────────────────────────┐
│ AutoConfigurationImportSelector │
│ │
│ Reads: │
│ META-INF/spring/ │
│ org.springframework.boot. │
│ autoconfigure. │
│ AutoConfiguration.imports │
└────────────┬─────────────────────┘
│ 140+ class names
▼
┌──────────────────────────────────┐
│ For each auto-config class: │
│ │
│ ┌─────────────────────────────┐ │
│ │ @ConditionalOnClass? │ │
│ │ Class on classpath? ────No──┼──┼──▶ SKIP
│ └─────────┬───────────────────┘ │
│ │ Yes │
│ ┌─────────▼───────────────────┐ │
│ │ @ConditionalOnProperty? │ │
│ │ Property set? ──────No──────┼──┼──▶ SKIP
│ └─────────┬───────────────────┘ │
│ │ Yes │
│ ┌─────────▼───────────────────┐ │
│ │ @ConditionalOnMissingBean? │ │
│ │ User defined own? ──Yes─────┼──┼──▶ BACK OFF
│ └─────────┬───────────────────┘ │
│ │ No │
│ ▼ │
│ ┌─────────────────────────────┐ │
│ │ CREATE BEANS │ │
│ │ Process @Bean methods │ │
│ └─────────────────────────────┘ │
└──────────────────────────────────┘
Key Takeaway
@EnableAutoConfiguration is the mechanism that makes Spring Boot feel magical. When you add a starter dependency like spring-boot-starter-web, auto-configuration detects the web classes on your classpath and creates an embedded Tomcat, a DispatcherServlet, and JSON serialization beans, all without any configuration from you.
The mechanism relies on conditional annotations. Each auto-configuration class declares conditions: @ConditionalOnClass checks if a library is present, @ConditionalOnProperty checks if a setting is enabled, and @ConditionalOnMissingBean checks if you have already defined your own bean. If all conditions pass, the auto-configuration creates its beans. If you define your own, it backs off.
In our National Bank of India audit project, we built a custom auto-configuration that created an AuditService bean only when bank.audit.enabled=true and no user-defined AuditService existed. When we defined our own Kafka-based AuditService, the auto-configured file-based one was skipped automatically.
At the senior level, know that auto-configuration classes are registered in AutoConfiguration.imports (not the deprecated spring.factories), that class-level conditions are evaluated before method-level conditions to avoid unnecessary class loading, and that debug=true prints the CONDITIONS EVALUATION REPORT showing every decision.
Remember one sentence: @EnableAutoConfiguration tells Spring Boot to look at your classpath and properties, then conditionally create beans you need while backing off when you define your own.
---