Autoconfiguration
Spring Boot’s autoconfiguration is what makes @SpringBootApplication feel like magic: add a
dependency to your classpath, and the beans you’d normally have to configure by hand — a
DataSource, a JdbcTemplate, a DispatcherServlet — simply appear, already wired into the same IoC
container described in IoC Containers.
How it decides what to configure
Each auto-configuration class is annotated with conditions such as:
@Configuration@ConditionalOnClass(DataSource.class)@ConditionalOnMissingBean(DataSource.class)public class DataSourceAutoConfiguration { @Bean public DataSource dataSource(DataSourceProperties properties) { return properties.initializeDataSourceBuilder().build(); }}@ConditionalOnClass— only apply if a given class is on the classpath (e.g. you added the JDBC driver dependency).@ConditionalOnMissingBean— only apply if you haven’t already defined your own bean of that type. Your explicit@Beanalways wins.
Overriding it
Define your own bean of the same type, and Spring Boot’s autoconfiguration backs off automatically:
@Configurationpublic class DataSourceConfig { @Bean public DataSource dataSource() { return DataSourceBuilder.create() .url("jdbc:postgresql://localhost/app") .build(); }}Disabling it entirely
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })public class Application { }Seeing what got configured
Add the Actuator dependency and hit /actuator/conditions in a running app — it lists every
auto-configuration class considered, and whether it matched or was excluded, which is invaluable
when something isn’t wired the way you expect.