批注接口 ConditionalOnClass


@Target({TYPE,METHOD}) @Retention(RUNTIME) @Documented @Conditional(org.springframework.boot.autoconfigure.condition.OnClassCondition.class) public @interface ConditionalOnClass
@Conditional that only matches when the specified classes are on the classpath.

A Class value can be safely specified on @Configuration classes as the annotation metadata is parsed by using ASM before the class is loaded. If a class reference cannot be used then a name String attribute can be used.

Note: Extra care must be taken when using @ConditionalOnClass on @Bean methods where typically the return type is the target of the condition. Before the condition on the method applies, the JVM will have loaded the class and potentially processed method references which will fail if the class is not present. To handle this scenario, a separate @Configuration class should be used to isolate the condition. For example:

 @AutoConfiguration
 public class MyAutoConfiguration {

        @Configuration(proxyBeanMethods = false)
        @ConditionalOnClass(SomeService.class)
        public static class SomeServiceConfiguration {

                @Bean
                @ConditionalOnMissingBean
                public SomeService someService() {
                        return new SomeService();
                }

        }

 }
从以下版本开始:
1.0.0
作者:
Phillip Webb
  • 可选元素概要

    可选元素
    修饰符和类型
    可选元素
    说明
    The classes names that must be present.
    Class<?>[]
    The classes that must be present.
  • 元素详细资料

    • value

      Class<?>[] value
      The classes that must be present. Since this annotation is parsed by loading class bytecode, it is safe to specify classes here that may ultimately not be on the classpath, only if this annotation is directly on the affected component and not if this annotation is used as a composed, meta-annotation. In order to use this annotation as a meta-annotation, only use the name() attribute.
      返回:
      the classes that must be present
      默认值:
      {}
    • name

      String[] name
      The classes names that must be present.
      返回:
      the class names that must be present.
      默认值:
      {}