Spring Boot auto-configuration is the feature that makes it so easy to get started. It automatically configures your application based on the libraries present on the classpath and your defined settings in application.properties
or application.yml
.
No XML. No manual wiring. Just focus on your business logic.
Example: If you add spring-boot-starter-web
, Spring Boot automatically configures:
- DispatcherServlet
- Jackson (for JSON)
- Tomcat (embedded)
- MVC infrastructure

🔍 How Does It Work Internally?
Auto-configuration uses:
- Spring Factories
- Conditional Annotations
- Classpath detection
@EnableAutoConfiguration
Let’s break it down.
🏁 Step 1: @SpringBootApplication
Enables Auto-Configuration
The @SpringBootApplication
annotation includes:
1 2 3 4 5 6 7 8 9 |
@SpringBootApplication // Equivalent to: @Configuration @EnableAutoConfiguration @ComponentScan |
Here, @EnableAutoConfiguration
triggers the magic. It tells Spring Boot to look for configuration classes and apply them based on what’s available.
🧠 Step 2: META-INF/spring.factories
Spring Boot looks at:
1 2 3 4 |
/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports (or older: spring.factories) |
This file contains entries like:
1 2 3 4 5 6 7 8 9 |
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ ... |
Spring loads each of these classes and checks if it should apply them using conditional annotations.
⚙️ Step 3: Conditional Configuration (The Brain Behind It)
Each auto-configuration class uses smart annotations to decide when to activate.
Common ones include:
Annotation | What It Checks |
---|---|
@ConditionalOnClass | Only loads if a class exists (e.g., DispatcherServlet ) |
@ConditionalOnMissingBean | Loads only if no other bean of that type is present |
@ConditionalOnProperty | Loads if a property is set (or not) |
@ConditionalOnWebApplication | Loads only for web applications |
@ConditionalOnBean | Loads only if another bean is present |
✅ This is why Spring Boot is “auto” — but still safe and controllable.
📦 Real-World Example: Spring Web Starter
If you add this dependency:
1 2 3 4 5 6 7 |
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> |
Then:
WebMvcAutoConfiguration
gets triggered- It sees
DispatcherServlet
is on classpath - It configures Spring MVC automatically
- You get a running web server without writing any config
🧰 Customizing Auto-Configuration
✅ Override a Bean
If Spring Boot autoconfigures a bean, but you define one manually, yours will take priority.
Example: To override Jackson’s object mapper:
1 2 3 4 5 6 7 8 9 |
@Bean public ObjectMapper customMapper() { ObjectMapper mapper = new ObjectMapper(); // custom config return mapper; } |
❌ Disable Auto-Config
You can disable a specific auto-config using:
1 2 3 4 |
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) |
Or, via application.properties
:
1 2 3 4 |
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration |
🔍 Debugging Auto-Configuration
Use Spring Boot Actuator or debug logs to see which configs were applied:
1 2 3 4 |
debug=true |
This will print lines like:
1 2 3 4 5 6 7 8 9 10 11 |
CONDITION EVALUATION REPORT --------------------------- Positive matches: - WebMvcAutoConfiguration matched - JacksonAutoConfiguration matched Negative matches: - MailSenderAutoConfiguration did not match |
🛠️ Tools That Use Auto-Configuration
- Spring Web
- Spring Data JPA
- Spring Security
- Spring Cache
- Spring Kafka
- Spring Mail
- Spring Actuator
Pretty much every starter uses this model.
🧪 Can I Write My Own Auto-Configuration?
Yes! You can create custom auto-configurations like this:
1 2 3 4 5 6 7 8 9 10 11 |
@Configuration @ConditionalOnProperty(name = "feature.enabled", havingValue = "true") public class MyFeatureAutoConfiguration { @Bean public MyService myService() { return new MyService(); } } |
Then register it in:
1 2 3 4 |
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports |
🧭 Summary
Feature | Purpose |
---|---|
@EnableAutoConfiguration | Entry point for auto-config |
spring.factories / imports | Lists all auto-config classes |
Conditional annotations | Decide whether to load config |
application.properties | Fine-tune or disable configs |
Manual Beans | Override defaults if needed |
✅ Why You Should Learn It
- Helps you debug startup issues
- Lets you override Spring’s behavior safely
- You’ll understand what’s going on “under the hood”
- Essential for building custom Spring Boot starters