@Target({ElementType.TYPE, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic@interface Conditional {
/**
* All {@link Condition} classes that must {@linkplain Condition#matches match}
* in order for the component to be registered.
*/
Class<? extendsCondition>[] value();
}
java
在使用时只需传入Condition接口的实现类即可。
在Condition接口中,只有一个matches方法:
@FunctionalInterfacepublicinterfaceCondition {
/**
* Determine if the condition matches.
* @param context the condition context
* @param metadata the metadata of the {@link org.springframework.core.type.AnnotationMetadata class}
* or {@link org.springframework.core.type.MethodMetadata method} being checked
* @return {@code true} if the condition matches and the component can be registered,
* or {@code false} to veto the annotated component's registration
*/booleanmatches(ConditionContext context, AnnotatedTypeMetadata metadata);
}
java
当matches返回true时,将会进行注册。
2. SpringBootCondition
SpringBootCondition可以配合日志系统,帮助我们查看加载了哪些类。
我们只需要实现一个方法即可:
/**
* Determine the outcome of the match along with suitable log output.
* @param context the condition context
* @param metadata the annotation metadata
* @return the condition outcome
*/publicabstract ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata);