What does the spring annotation ConditionalOnMissingBean do?
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
Overview
In the Spring Framework, annotations play a critical role in defining metadata about the beans and how they are wired. One such useful annotation is `@ConditionalOnMissingBean`, which falls under the class of conditional annotations in Spring Boot. Conditional annotations allow you to create more flexible configurations by determining the bean creation based on certain conditions.
Understanding `@ConditionalOnMissingBean`
`@ConditionalOnMissingBean` is a Spring Boot annotation that allows you to specify that a bean should only be created if a bean of the specified type does not already exist in the application context. This is particularly useful for providing default beans that can be overridden by user-defined configurations.
Technical Explanation
When Spring processes the beans during the configuration phase, it evaluates the condition specified by the `@ConditionalOnMissingBean` annotation. If the condition is true, the bean gets added to the application context; otherwise, it's not created.
The annotation can be applied at the method or class level, typically in configuration classes annotated with `@Configuration`.
Annotation `Parameters`
The `@ConditionalOnMissingBean` annotation has several parameters that allow you to specify the conditions under which the bean will be loaded:
- value: Specifies the type(s) of the bean(s) that must be missing for the annotated bean to be added.
- name: If you want to conditionally create the bean based on the absence of a specific bean by name.
- annotation: Check if no bean is present with the specified annotation.
- type: Like `value`, specifies the types required to be missing, but can handle generic types.
Example Usage
Suppose you have a requirement to provide a default `DataSource` bean in your Spring Boot application only if one hasn't been provided by the user.
- Custom Conditions: You can create custom conditions by implementing the `Condition` interface, giving you even more control over the bean creation process.
- Bean Overriding: With `@ConditionalOnMissingBean`, users can easily override the default configuration provided by a library or third-party module.
Related reading
- What exactly is Field Injection and how to avoid it?
- What is a difference between ? super E and ? extends E?
- What is a raw type and why shouldn't we use it?
- What is a singleton in C?
- What does the 'static' keyword do in a class?
- What does the static modifier after import mean?
- What is Bulkhead Pattern used by Hystrix?
- What is lazy initialization and why is it useful?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.