Spring Boot Auto-configuration
Auto-configuration is Spring Boot’s superpower — it automatically configures beans based on what’s on your classpath. Add spring-boot-starter-web and you get an embedded Tomcat, Jackson for JSON, and Spring MVC — all configured and ready.
How It Works
1. You add spring-boot-starter-data-jpa to pom.xml
2. Spring Boot sees Hibernate + DataSource on classpath
3. Auto-configuration kicks in:
→ Creates a DataSource bean (HikariCP)
→ Creates an EntityManagerFactory
→ Creates a TransactionManager
→ Enables Spring Data JPA repositories
4. You write ZERO configuration — it just works
The Magic Behind It
Spring Boot checks conditions before creating each bean:
// Simplified auto-config class
@Configuration
@ConditionalOnClass(DataSource.class) // Only if DataSource is on classpath
@ConditionalOnProperty(name = "spring.datasource.url") // Only if URL is configured
public class DataSourceAutoConfiguration {
@Bean
@ConditionalOnMissingBean // Only if user hasn't defined their own
public DataSource dataSource(DataSourceProperties properties) {
return HikariDataSource(properties);
}
}
Conditional Annotations
| Annotation | Condition |
|---|---|
@ConditionalOnClass |
Class exists on classpath |
@ConditionalOnMissingClass |
Class does NOT exist |
@ConditionalOnBean |
A specific bean exists |
@ConditionalOnMissingBean |
No bean of this type exists |
@ConditionalOnProperty |
A property is set |
@ConditionalOnWebApplication |
It’s a web application |
Key rule: If you define your own bean, auto-configuration backs off (
@ConditionalOnMissingBean).
Viewing the Auto-Configuration Report
./mvnw spring-boot:run --debug
# or
java -jar app.jar --debug
# or set in application.properties
debug=true
Output:
============================
CONDITIONS EVALUATION REPORT
============================
Positive matches:
-----------------
DataSourceAutoConfiguration matched:
- @ConditionalOnClass found required classes 'javax.sql.DataSource'
- @ConditionalOnProperty (spring.datasource.url) matched
Negative matches:
-----------------
MongoAutoConfiguration:
- @ConditionalOnClass did not find required class 'com.mongodb.client.MongoClient'
Overriding Auto-Configuration
Option 1: Define Your Own Bean
@Configuration
public class CustomDataSourceConfig {
@Bean
public DataSource dataSource() {
// Your custom DataSource — auto-config won't create one
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:postgresql://localhost:5432/mydb");
ds.setUsername("admin");
ds.setMaximumPoolSize(20);
return ds;
}
}
Option 2: Exclude Auto-Configuration
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
SecurityAutoConfiguration.class
})
public class BookstoreApiApplication { }
Or in properties:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
What Auto-Configuration Gives You
| Starter | Auto-Configured Beans |
|---|---|
spring-boot-starter-web |
Embedded Tomcat, DispatcherServlet, Jackson, error handling |
spring-boot-starter-data-jpa |
DataSource, EntityManager, TransactionManager, repositories |
spring-boot-starter-security |
SecurityFilterChain, default login, CSRF protection |
spring-boot-starter-actuator |
Health endpoint, metrics, info |
spring-boot-starter-cache |
CacheManager |
Common Mistakes
| Mistake | Fix |
|---|---|
| “Why is security blocking everything?” | spring-boot-starter-security auto-configures auth — configure or exclude it |
| “H2 console not working” | Enable: spring.h2.console.enabled=true |
| Duplicate bean errors | You defined a bean AND auto-config created one — use @ConditionalOnMissingBean |
Key Takeaways
- Auto-configuration creates beans conditionally — based on classpath, properties, and existing beans.
- Your custom beans always win over auto-configured ones.
- Use
--debugto see what was auto-configured and why. - Use
excludeto disable specific auto-configurations. - Starters bundle dependencies + auto-configuration together.
What’s Next?
Next: Dependency Injection and IoC — the core pattern that powers everything in Spring.