Auto Configuration
Understand how Spring Boot's auto-configuration works under the hood — how it detects your classpath, applies conditional configuration, and how to customize or disable it.
Last reviewed: January 2025
🔍 What Is It?
Auto-configuration is a set of @Configuration classes that Spring Boot activates conditionally based on what's on your classpath, what beans you've already defined, and what properties you've set.
How Spring Boot discovers auto-configurations:
- →Spring Boot 2.x:
META-INF/spring.factoriesfile - →Spring Boot 3.x:
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
Key conditional annotations:
- →
@ConditionalOnClass— activate only if a class is on the classpath - →
@ConditionalOnMissingBean— activate only if you haven't defined your own bean - →
@ConditionalOnProperty— activate only if a property is set - →
@ConditionalOnWebApplication— activate only in web apps - →
@ConditionalOnExpression— activate based on SpEL expression
This means auto-configuration is never forced on you — define your own bean and Spring backs off.
🎯 Why Do We Need It?
Without auto-configuration, every Spring Boot project would require:
@Bean
public DataSource dataSource() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:postgresql://localhost:5432/mydb");
// ... 10 more lines
return ds;
}
// Plus EntityManagerFactory, TransactionManager, etc.Auto-configuration does all this from spring.datasource.* properties. You write 3 lines of config instead of 50 lines of code.
🏗️ What Happens in Real Projects?
Every Spring Boot application uses auto-configuration constantly:
- →Add
spring-boot-starter-web→ DispatcherServlet, Jackson, Tomcat auto-configured - →Add
spring-boot-starter-data-jpa+ DB driver → DataSource, JPA, Transactions auto-configured - →Add
spring-boot-starter-security→ Security filter chain auto-configured
In enterprise projects, teams write custom auto-configurations for shared libraries that auto-configure themselves when added as a dependency.
⚙️ How It Works
Step-by-step auto-configuration process:
@EnableAutoConfigurationtriggersAutoConfigurationImportSelector- Selector reads
AutoConfiguration.imports— a list of 150+ configuration classes - Each config class is evaluated against its
@Conditionalannotations - Passing configs are imported into the ApplicationContext
@ConditionalOnMissingBeanensures your beans take priority
Debug auto-configuration:
debug=true
# Or: GET /actuator/conditions💻 Example
Understanding and Customizing Auto-Configuration — How to inspect, override, and write your own auto-configuration.
🤔 Common Confusions
❓ Does auto-configuration override my beans?
Never. Auto-configuration uses @ConditionalOnMissingBean — it only activates if you haven't defined that bean yourself. Your beans always win.
❓ How do I know which auto-configurations are active?
Run with --debug flag or set debug=true in application.properties. Spring prints a "CONDITIONS EVALUATION REPORT" showing every auto-config class and whether it was applied or skipped.
❓ spring.factories vs AutoConfiguration.imports — what changed?
Spring Boot 3.x moved to META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports for better performance. Spring Boot 2.7 supports both. Use the new format for Boot 3.x custom starters.
🚨 Common Production Issues
Security auto-configuration activating on a non-web service — add SecurityAutoConfiguration to exclude list
Multiple DataSource auto-configurations conflicting — explicitly mark one @Primary
Auto-configured connection pool too small — always override HikariCP settings for production
Jackson auto-configuration serializing nulls — configure ObjectMapper via spring.jackson.* properties
✅ Best Practices
Use debug=true during development to understand which auto-configs are active
Prefer property-based customization over overriding beans when possible
When writing shared libraries, use @ConditionalOnMissingBean so consumers can override
Document which auto-configurations your custom starter provides
🏛️ Architect's Note
Auto-configuration is the foundation of Spring Boot's productivity. When building internal platform libraries, write them as Spring Boot starters with auto-configuration. Teams add your library as a dependency and it just works — zero configuration required. This is exactly how spring-boot-starter-* libraries work.
💬 Frequently Asked Questions
🎯 Key Takeaways
🔗 Related Topics
🎯 Practice this topic in interviews
↗ Where this topic fits
Spring Boot Technology Guide
Return to the Spring Boot learning hub.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Sources
Sources / References
Use these Spring Boot references to verify the concept in official docs and continue into the most relevant SplashRide pages.
Official documentation
Related SplashRide pages
SplashRide Spring Boot learning path
Browse the full Spring Boot topic collection and learning order on SplashRide.
Spring Boot Interview Prep
Practice Spring Boot interview questions and scenario-based answers.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Spring Boot Introduction
Understand what Spring Boot is, why it exists, and how it transforms enterprise Java development by eliminating boilerplate configuration.
Starter Dependencies
Master Spring Boot Starters — the curated dependency bundles that eliminate version conflicts and bring everything you need for a feature with a single dependency.