What is auto-configuration in Spring Boot?
Answer
Auto-configuration is Spring Boot's core feature that automatically configures Spring beans based on: (1) Dependencies on the classpath; (2) Existing beans in the application context; (3) Properties defined in application.properties/yaml. How it works: Spring Boot includes over 100 auto-configuration classes annotated with @Configuration. Each uses @Conditional annotations to activate only when specific conditions are met. Example: DataSourceAutoConfiguration fires when spring-jdbc is on the classpath AND no DataSource bean is defined. @SpringBootApplication meta-annotation includes @EnableAutoConfiguration which imports AutoConfigurationImportSelector — reads all auto-configuration class names from META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports and applies conditional ones. Conditional annotations: @ConditionalOnClass(DataSource.class) — active only if DataSource class is on classpath; @ConditionalOnMissingBean(DataSource.class) — active only if no DataSource bean exists (allows override); @ConditionalOnProperty("spring.jpa.enabled"); @ConditionalOnWebApplication. Seeing what's configured: run with --debug flag or spring.boot.admin.server.enabled=true — prints conditions evaluation report. Overriding: define your own bean of the same type → auto-configured bean is disabled. Use @SpringBootTest to test auto-configuration behavior.
Previous
What is the difference between Spring and Spring Boot?
Next
What is @SpringBootApplication annotation?