"How does Spring Boot's auto-configuration work under the hood?"
Auto-configuration is what makes Spring Boot feel magical, and interviewers use it to separate Spring Boot users from Spring Boot knowers. The answer requires understanding classpath scanning, SPI-style loading, conditional bean registration, and the order-of-operations — it's a deep cut that signals real framework literacy.
Start at the entry point: @SpringBootApplication is a meta-annotation combining @Configuration, @ComponentScan, and @EnableAutoConfiguration.
@EnableAutoConfiguration imports AutoConfigurationImportSelector. This selector reads META-INF/spring.factories (or META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports in Boot 2.7+) from every jar on the classpath.
For each declared auto-configuration class, Spring registers it conditionally via @Conditional-family annotations: @ConditionalOnClass (only if some class is on the classpath), @ConditionalOnMissingBean (only if the user hasn't defined that bean themselves), @ConditionalOnProperty (only if a specific config property is set), etc.
Beans declared inside auto-configs respect user overrides: if you define your own DataSource @Bean, @ConditionalOnMissingBean ensures Spring Boot's default DataSourceAutoConfiguration doesn't register a competing one.
Debug tip: run the app with --debug to print an AUTO-CONFIGURATION REPORT listing every config class, whether it matched, and if not which condition failed. This is the single best way to understand what's actually loaded.
Spring Boot's magic is layered. @SpringBootApplication is a meta-annotation for three things: @Configuration (this class defines beans), @ComponentScan (scan the package for @Component, @Service, etc.), and @EnableAutoConfiguration (the magic bit). @EnableAutoConfiguration uses @Import(AutoConfigurationImportSelector.class). AutoConfigurationImportSelector is a DeferredImportSelector that runs after all user @Configuration classes have been processed. It loads META-INF/spring.factories from every jar on the classpath — in Boot 2.7+ the location moved to META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. Each starter jar declares its auto-configuration classes here: for example, spring-boot-starter-web's jar declares WebMvcAutoConfiguration, DispatcherServletAutoConfiguration, and so on. But declaring doesn't mean it runs — each auto-config class is heavily annotated with conditions: @ConditionalOnClass(DispatcherServlet.class) means it only activates if Spring MVC is on the classpath; @ConditionalOnMissingBean means it only creates a default bean if the user hasn't declared their own; @ConditionalOnProperty("spring.datasource.url") means it only triggers when a specific property is set. This three-way interplay — classpath presence, user-declared beans, and properties — is what gives Spring Boot its "configures itself and gets out of your way" feel. When you add a database driver to pom.xml, DataSourceAutoConfiguration's @ConditionalOnClass(DataSource.class) turns true; it autoconfigures a HikariCP DataSource using spring.datasource.* properties. If you declare your own DataSource bean, @ConditionalOnMissingBean skips the default. The single most useful debugging tool is --debug on startup, which prints an AUTO-CONFIGURATION REPORT — every candidate auto-config and whether it matched, plus the exact condition that failed if it didn't. When a "why isn't my DataSource configured" issue comes up, that report answers it in 30 seconds.
Name specific conditional annotations (@ConditionalOnClass, @ConditionalOnMissingBean, @ConditionalOnProperty) — generic answers lose points.
Mention the Boot 2.7+ move from spring.factories to AutoConfiguration.imports — this signals you've tracked recent versions.
--debug / AUTO-CONFIGURATION REPORT as a practical debugging tip is a strong differentiator.
If you blank on the exact file name, HireMe AI can surface spring.factories / AutoConfiguration.imports on demand.
@Configuration is a user-declared config class that Spring loads unconditionally. Auto-configuration classes are registered conditionally by Spring Boot based on classpath and properties.
Yes: @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) or the property spring.autoconfigure.exclude.
Because auto-configs run via DeferredImportSelector — they're processed after user @Configuration, so by the time conditionals evaluate, user beans are already known.
HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.
Try HireMe AI Free