org.springframework.boot.autoconfigure.condition

Annotation Type ConditionalOnBean



  • @Target(value={TYPE,METHOD})
     @Retention(value=RUNTIME)
     @Documented
     @Conditional(value=org.springframework.boot.autoconfigure.condition.OnBeanCondition.class)
    public @interface ConditionalOnBean
    Conditional that only matches when the specified bean classes and/or names are 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 {
    
         @ConditionalOnBean
         @Bean
         public MyService myService() {
             ...
         }
    
     }

    In the sample above the condition will match if a 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.

    Author:
    Phillip Webb
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element and Description
      Class<? extends Annotation>[] annotation
      The annotation type decorating a bean that should be checked.
      String[] name
      The names of beans to check.
      SearchStrategy search
      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.
    • Element Detail

      • value

        public abstract Class<?>[] value
        The class type of bean that should be checked. The condition matches when all of the classes specified are contained in the ApplicationContext.
        Returns:
        the class types of beans to check
        Default:
        {}
      • type

        public abstract String[] type
        The class type names of bean that should be checked. The condition matches when all of the classes specified are contained in the ApplicationContext.
        Returns:
        the class type names of beans to check
        Default:
        {}
      • annotation

        public abstract 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 the ApplicationContext.
        Returns:
        the class-level annotation types to check
        Default:
        {}
      • name

        public abstract String[] name
        The names of beans to check. The condition matches when all of the bean names specified are contained in the ApplicationContext.
        Returns:
        the name of beans to check
        Default:
        {}
      • search

        public abstract 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