Spring Boot's auto-configuration is a frequent interview topic. Many people can only say "convention over configuration," but freeze when asked about the underlying principles.
The Three-in-One @SpringBootApplication
@SpringBootConfiguration // Equivalent to @Configuration
@EnableAutoConfiguration // Entry point for auto-configuration
@ComponentScan // Scans components
The key is @EnableAutoConfiguration.
The Auto-Configuration Flow
@EnableAutoConfigurationimportsAutoConfigurationImportSelector- It reads
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports(the Spring Boot 3 approach) - This file lists all candidate auto-configuration classes (about 200 of them)
- They are filtered through
@Conditionalannotations — loaded only if conditions are met
The Magic of @Conditional
Spring Boot provides many conditional annotations:
@ConditionalOnClass: Loads only if a certain class exists on the classpath. For example,DataSourceAutoConfigurationrequires the DataSource class to be present@ConditionalOnMissingBean: Auto-creates a bean only if the user hasn't defined it themselves@ConditionalOnProperty: Loads only if a certain property exists in the configuration file
How to Answer in an Interview
Spring Boot triggers auto-configuration through
@EnableAutoConfiguration. At startup, it scans theAutoConfiguration.importsfile and decides whether to load a given auto-configuration class based on@Conditionalcondition annotations. The end result: you addspring-boot-starter-web, and it automatically configures DispatcherServlet, Tomcat, and Jackson for you.
How to Verify Your Understanding
Create an empty Spring Boot project, open the spring-boot-autoconfigure jar, and take a look at what the AutoConfiguration.imports file looks like. Pick an auto-configuration class, click into it, and examine the @Conditional annotations on it.
Hands-on practice beats rote memorization every time.
Comments
Comments are closed.