Annotation Interface ConditionalOnMissingBean
@Target({TYPE,METHOD})
@Retention(RUNTIME)
@Documented
@Conditional(org.springframework.boot.autoconfigure.condition.OnBeanCondition.class)
public @interface ConditionalOnMissingBean
@Conditional
that only matches when no beans meeting the specified
requirements are already contained in the BeanFactory
. None of the requirements
must be met for the condition to match and the requirements do not have to be met by
the same bean.
When placed on a @Bean
method and none of value()
, type()
,
name()
, or annotation()
has been specified, the bean type to match
defaults to the return type of the @Bean
method:
@Configuration public class MyAutoConfiguration { @ConditionalOnMissingBean @Bean public MyService myService() { ... } }
In the sample above the condition will match if no bean of type MyService
is
already contained in the BeanFactory
.
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.
- Since:
- 1.0.0
- Author:
- Phillip Webb, Andy Wilkinson
-
Optional Element Summary
Modifier and TypeOptional ElementDescriptionClass<? extends Annotation>[]
The annotation type decorating a bean that should be checked.Class<?>[]
The class types of beans that should be ignored when identifying matching beans.String[]
The class type names of beans that should be ignored when identifying matching beans.String[]
The names of beans to check.Class<?>[]
Additional classes that may contain the specified bean types within their generic parameters.Strategy to decide if the application context hierarchy (parent contexts) should be considered.String[]
The class type names of beans that should be checked.Class<?>[]
The class types of beans that should be checked.
-
Element Details
-
value
Class<?>[] valueThe class types of beans that should be checked. The condition matches when no bean of each class specified is contained in theBeanFactory
. Beans that are not autowire candidates or that are not default candidates are ignored.- Returns:
- the class types of beans to check
- See Also:
- Default:
- {}
-
type
String[] typeThe class type names of beans that should be checked. The condition matches when no bean of each class specified is contained in theBeanFactory
. Beans that are not autowire candidates or that are not default candidates are ignored.- Returns:
- the class type names of beans to check
- See Also:
- Default:
- {}
-
ignored
Class<?>[] ignoredThe class types of beans that should be ignored when identifying matching beans.- Returns:
- the class types of beans to ignore
- Since:
- 1.2.5
- Default:
- {}
-
ignoredType
String[] ignoredTypeThe class type names of beans that should be ignored when identifying matching beans.- Returns:
- the class type names of beans to ignore
- Since:
- 1.2.5
- Default:
- {}
-
annotation
Class<? extends Annotation>[] annotationThe annotation type decorating a bean that should be checked. The condition matches when each annotation specified is missing from all beans in theBeanFactory
. Beans that are not autowire candidates or that are not default candidates are ignored.- Returns:
- the class-level annotation types to check
- See Also:
- Default:
- {}
-
name
String[] nameThe names of beans to check. The condition matches when each bean name specified is missing in theBeanFactory
.- Returns:
- the names of beans to check
- Default:
- {}
-
search
SearchStrategy searchStrategy to decide if the application context hierarchy (parent contexts) should be considered.- Returns:
- the search strategy
- Default:
- ALL
-
parameterizedContainer
Class<?>[] parameterizedContainerAdditional 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:
- {}
-