@ConditionalOnMissingBean
`@ConditionalOnMissingBean` lets Spring Boot create a bean only when no matching bean is already available in the application context. It is commonly used by auto-configuration to provide sensible defaults while allowing application code to override them.
`@ConditionalOnMissingBean` is a Spring Boot condition used on configuration classes or bean methods. The guarded configuration is applied only when Spring Boot determines that a matching bean is missing from the relevant application context.
A common use is defining a default implementation in auto-configuration. For example, a library can register a default `PaymentService`, while an application can define its own `PaymentService`; the condition prevents the library's default from being created when the application already supplies a suitable bean.
The condition can match by bean type, name, or other supported criteria. The exact matching behavior depends on the attributes used and the point in configuration processing where the condition is evaluated.
This mechanism is different from replacing an existing bean after creation: the conditional configuration is simply not applied when its condition is not satisfied. This makes it a key part of Spring Boot's convention-over-configuration approach.
Key points
- Provides a default bean when a matching bean is absent.
- Frequently used in Spring Boot auto-configuration.
- Allows application-defined beans to override library defaults.
- Can be applied to configuration classes or bean methods.
- Matching can be constrained by type, name, or other condition attributes.
Interview questions
How would you use `@ConditionalOnMissingBean` in a Spring Boot auto-configuration?
I would define the library's default implementation in an auto-configuration class and guard its `@Bean` method with `@ConditionalOnMissingBean`. When an application supplies a compatible bean, the condition prevents the default bean from being registered. Otherwise, the application receives the library's default behavior without additional configuration.
What problem does `@ConditionalOnMissingBean` solve in Spring Boot?
It lets a library or auto-configuration provide a default bean without preventing an application from defining its own implementation. If a matching application bean already exists, the conditional bean definition is not applied. This gives application code a natural way to customize framework defaults.
How is `@ConditionalOnMissingBean` different from `@Primary`?
`@ConditionalOnMissingBean` determines whether conditional configuration should create a bean based on whether a matching bean is already present. `@Primary` does not prevent bean creation; it influences which candidate Spring prefers when multiple beans are eligible for injection. Therefore, the two solve different problems: conditional registration versus dependency-selection preference.
Can `@ConditionalOnMissingBean` match by something other than a bean type?
Yes. The annotation supports different matching criteria, including bean types and bean names, through its attributes. The chosen criteria determine what Spring Boot considers to be a matching existing bean. When diagnosing unexpected condition results, I would inspect the condition attributes and the auto-configuration condition evaluation report rather than assuming every bean is matched solely by type.