Annotation Type ConditionalOnBean
@Target({TYPE,METHOD}) @Retention(RUNTIME) @Documented @Conditional(org.springframework.boot.autoconfigure.condition.OnBeanCondition.class) public @interface ConditionalOnBean
Conditional
that only matches when beans meeting all the specified requirements are already contained in theBeanFactory
. All the requirements must be met for the condition to match, but they do not have to be met by the same bean.When placed on a
@Bean
method, the bean class defaults to the return type of the factory method:@Configuration public class MyAutoConfiguration { @ConditionalOnBean @Bean public MyService myService() { ... } }
In the sample above the condition will match if a bean of type
MyService
is already contained in theBeanFactory
.The condition can only match the bean definitions that have been processed by the application context so far and, as such, it is strongly recommended to use this condition on auto-configuration classes only. If a candidate bean may be created by another auto-configuration, make sure that the one using this condition runs after.
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description Class<? extends Annotation>[]
annotation
The annotation type decorating a bean that should be checked.String[]
name
The names of beans to check.Class<?>[]
parameterizedContainer
Additional classes that may contain the specified bean types within their generic parameters.SearchStrategy
search
Strategy to decide if the application context hierarchy (parent contexts) should be considered.String[]
type
The class type names of beans that should be checked.Class<?>[]
value
The class types of beans that should be checked.
annotation
Class<? extends Annotation>[] annotation
The annotation type decorating a bean that should be checked. The condition matches when all of the annotations specified are defined on beans in theBeanFactory
.- Returns:
- the class-level annotation types to check
- Default:
- {}
search
SearchStrategy search
Strategy to decide if the application context hierarchy (parent contexts) should be considered.- Returns:
- the search strategy
- Default:
- org.springframework.boot.autoconfigure.condition.SearchStrategy.ALL
parameterizedContainer
Class<?>[] parameterizedContainer
Additional classes that may contain the specified bean types within their generic parameters. For example, an annotation declaringvalue=Name.class
andparameterizedContainer=NameRegistration.class
would detect bothName
andNameRegistration<Name>
.- Returns:
- the container types
- Since:
- 2.1.0
- Default:
- {}