Spring Boot auto-configuration is designed to provide default beans automatically. However, if a developer defines a custom bean of the same type, the auto-configuration should "back away" and allow the custom bean to take precedence. This behavior is commonly achieved using the @ConditionalOnMissingBean annotation.
Suppose Spring Boot provides a default EmailService bean. If the application developer creates a custom EmailService bean, Spring Boot should not create the default bean again.
@Configuration
public class EmailAutoConfiguration {
@Bean
@ConditionalOnMissingBean(
EmailService.class)
public EmailService emailService() {
return new DefaultEmailService();
}
}@Configuration
public class CustomConfig {
@Bean
public EmailService emailService() {
return new CustomEmailService();
}
}Application Startup
↓
Check Existing Beans
↓
EmailService Exists?
/ \
Yes No
↓ ↓
Skip Bean Create Bean
@Configuration
public class DataSourceAutoConfiguration {
@Bean
@ConditionalOnMissingBean(
DataSource.class)
public DataSource dataSource() {
// Default DataSource
}
}@Bean
public DataSource customDataSource() {
return new HikariDataSource();
}Spring Boot will not create its default DataSource because a custom one already exists.
When creating custom Spring Boot starters or auto-configurations, use @ConditionalOnMissingBean to provide sensible defaults while still allowing developers to override them easily.
A concise interview answer is: To make an auto-configuration back away when a bean already exists, Spring Boot uses the @ConditionalOnMissingBean annotation. It tells Spring to create the bean only if no bean of the specified type is present in the application context. This allows developers to provide custom implementations while preventing conflicts with default auto-configured beans.
Last updated: 11 November 2026
Next question: How do you deploy a Spring Boot web application as a JAR or WAR file?