Spring Boot's auto-configuration mechanism inspects the classpath and existing bean definitions at startup to automatically wire up sensible default configuration, which you can then override.
Key Points: • Driven by @EnableAutoConfiguration (pulled in via @SpringBootApplication), which loads configuration classes listed in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. • Each auto-configuration class is guarded by conditional annotations like @ConditionalOnClass and @ConditionalOnMissingBean, so it only activates when relevant and backs off if you've defined your own bean. • Defining your own @Bean of the same type automatically takes precedence over the auto-configured default. • Specific auto-configurations can be excluded via the exclude attribute or spring.autoconfigure.exclude property. • Properties in application.yml (e.g. spring.datasource.*) further customize the auto-configured beans without needing to replace them.
Example: If spring-boot-starter-data-jpa is on the classpath and no DataSource bean exists yet, DataSourceAutoConfiguration creates one automatically from spring.datasource.* properties; defining your own DataSource bean disables that default.
Code Example:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}Interview Tip: A concise interview answer is:
"Auto-configuration scans the classpath at startup and, using @ConditionalOnClass and @ConditionalOnMissingBean, wires up sensible default beans only when they're needed and not already defined. You override it either by defining your own bean of that type, setting configuration properties, or explicitly excluding an auto-configuration class."