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.
Key Points: • @ConditionalOnMissingBean creates a bean only if no matching bean already exists in the Spring container. • It allows developers to override Spring Boot's default configuration with custom implementations. • This mechanism is heavily used in Spring Boot auto-configuration classes to provide flexibility and customization.
Example: 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.
Code Example:
@Configuration
public class EmailAutoConfiguration {
@Bean
@ConditionalOnMissingBean(EmailService.class)
public EmailService emailService() {
return new DefaultEmailService();
}
}Custom Bean:
@Configuration
public class CustomConfig {
@Bean
public EmailService emailService() {
return new CustomEmailService();
}
}Result:
• CustomEmailService is registered. • DefaultEmailService is not created. • Auto-configuration automatically backs away.
How It Works:
Application Startup ↓ Check Existing Beans ↓ EmailService Exists? / \ Yes No ↓ ↓ Skip Bean Create Bean
Common Conditional Annotations Used in Auto-Configuration:
• @ConditionalOnMissingBean - Create bean only if it does not already exist.
• @ConditionalOnBean - Create bean only if another bean exists.
• @ConditionalOnClass - Create bean only if a class is present in the classpath.
• @ConditionalOnProperty - Create bean based on property values.
Real-World Example:
Spring Boot DataSource Auto-Configuration:
@Configuration
public class DataSourceAutoConfiguration {
@Bean
@ConditionalOnMissingBean(DataSource.class)
public DataSource dataSource() {
// Default DataSource
}
}If the developer defines:
@Bean
public DataSource customDataSource() {
return new HikariDataSource();
}Spring Boot will not create its default DataSource because a custom one already exists.
Benefits:
• Supports customization without modifying framework code. • Prevents duplicate bean creation. • Makes auto-configuration flexible and extensible. • Allows application-specific implementations to override defaults.
Best Practice:
When creating custom Spring Boot starters or auto-configurations, use @ConditionalOnMissingBean to provide sensible defaults while still allowing developers to override them easily.
Interview Tip: 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.