Reference: Spring Docs - @ConditionalOnClass
- Registers a Bean if a specific Class file exists
- Why use it!
- The class where this annotation is written does not have control over the dependency
- Add the annotation at the Class Level so that the configuration Class itself can be activated/deactivated based on the existence of a specific class
- Add the annotation at the Method Level to specify conditions under which the Bean containing that method exists on the Classpath
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnClass {
/**
* 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
* <b>not</b> if this annotation is used as a composed, meta-annotation. In order to
* use this annotation as a meta-annotation, only use the {@link #name} attribute.
* @return the classes that must be present
*/
Class<?>[] value() default {};
/**
* The classes names that must be present.
* @return the class names that must be present.
*/
String[] name() default {};
}value- Class<?>[]- Specifies the classes that must exist
name- String[]- Specifies the class names that must exist