Annotation Interface ConditionalOnBean


@Documented @Retention(RUNTIME) @Conditional(cn.taketoday.context.condition.OnBeanCondition.class) @Target({TYPE,METHOD}) public @interface ConditionalOnBean
@Conditional that only matches when beans meeting all the specified requirements are already contained in the BeanFactory. 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 @Component method, the bean class defaults to the return type of the factory method:

 @Configuration
 public class MyAutoConfiguration {

     @Component
     @ConditionalOnBean
     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.

从以下版本开始:
4.0
作者:
Harry Yang 2021/11/25 21:32
  • 可选元素概要

    可选元素
    修饰符和类型
    可选元素
    说明
    Class<? extends Annotation>[]
    The annotation type decorating a bean that should be checked.
    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.
    The class type names of beans that should be checked.
    Class<?>[]
    The class types of beans that should be checked.
  • 元素详细资料

    • value

      Class<?>[] value
      The class types of beans that should be checked. The condition matches when beans of all classes specified are contained in the BeanFactory.
      返回:
      the class types of beans to check
      默认值:
      {}
    • type

      String[] type
      The class type names of beans that should be checked. The condition matches when beans of all classes specified are contained in the BeanFactory.
      返回:
      the class type names of beans to check
      默认值:
      {}
    • 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 the BeanFactory.
      返回:
      the class-level annotation types to check
      默认值:
      {}
    • name

      String[] name
      The names of beans to check. The condition matches when all of the bean names specified are contained in the BeanFactory.
      返回:
      the names of beans to check
      默认值:
      {}
    • search

      Strategy to decide if the application context hierarchy (parent contexts) should be considered.
      返回:
      the search strategy
      默认值:
      ALL
    • parameterizedContainer

      Class<?>[] parameterizedContainer
      Additional classes that may contain the specified bean types within their generic parameters. For example, an annotation declaring value=Name.class and parameterizedContainer=NameRegistration.class would detect both Name and NameRegistration<Name>.
      返回:
      the container types
      默认值:
      {}