@Target(value={TYPE,METHOD}) @Retention(value=RUNTIME) @Documented @Conditional(value=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. This only holds true if @ConditionalOnClass
is used on a
class. Extra care must be taken when using @ConditionalOnClass
on @Bean
methods: the value
attribute must not be used, instead the
name
attribute can be used to reference the class which must be present
as a String
. Alternatively create a separate @Configuration
class that
isolates 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(); } } }
public abstract Class<?>[] value
ConditionalOnClass
at class level, but it must not be used when using
ConditionalOnClass
on a @Bean
method.
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.
public abstract String[] name