注释类型 ConditionalOnMissingBean


  • @Target({TYPE,METHOD})
    @Retention(RUNTIME)
    @Documented
    @Conditional(org.springframework.boot.autoconfigure.condition.OnBeanCondition.class)
    public @interface ConditionalOnMissingBean
    Conditional that only matches when the specified bean classes and/or names are not already contained in the BeanFactory.

    When placed on a @Bean method, the bean class defaults to the return type of the factory 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.

    • 可选元素概要

      可选元素 
      修饰符和类型可选元素说明
      Class<? extends Annotation>[]annotation
      The annotation type decorating a bean that should be checked.
      Class<?>[]ignored
      The class type of beans that should be ignored when identifying matching beans.
      String[]ignoredType
      The class type names of beans that should be ignored when identifying matching beans.
      String[]name
      The names of beans to check.
      SearchStrategysearch
      Strategy to decide if the application context hierarchy (parent contexts) should be considered.
      String[]type
      The class type names of bean that should be checked.
      Class<?>[]value
      The class type of bean that should be checked.
    • 元素详细资料

      • value

        Class<?>[] value
        The class type of bean that should be checked. The condition matches when each class specified is missing in the ApplicationContext.
        返回:
        the class types of beans to check
        默认值:
        {}
      • type

        String[] type
        The class type names of bean that should be checked. The condition matches when each class specified is missing in the ApplicationContext.
        返回:
        the class type names of beans to check
        默认值:
        {}
      • ignored

        Class<?>[] ignored
        The class type of beans that should be ignored when identifying matching beans.
        返回:
        the class types of beans to ignore
        从以下版本开始:
        1.2.5
        默认值:
        {}
      • ignoredType

        String[] ignoredType
        The class type names of beans that should be ignored when identifying matching beans.
        返回:
        the class type names of beans to ignore
        从以下版本开始:
        1.2.5
        默认值:
        {}
      • annotation

        Class<? extends Annotation>[] annotation
        The annotation type decorating a bean that should be checked. The condition matches when each annotation specified is missing from all beans in the ApplicationContext.
        返回:
        the class-level annotation types to check
        默认值:
        {}
      • name

        String[] name
        The names of beans to check. The condition matches when each bean name specified is missing in the ApplicationContext.
        返回:
        the name of beans to check
        默认值:
        {}
      • search

        SearchStrategy search
        Strategy to decide if the application context hierarchy (parent contexts) should be considered.
        返回:
        the search strategy
        默认值:
        org.springframework.boot.autoconfigure.condition.SearchStrategy.ALL