1.spring boot的@ConditionalOn注解
参考:https://www.jianshu.com/p/30b2ff83ffeb
在spring boot中,@ConditionalOn系列注解都是自定义的条件注解,使用非常频繁.
参考:https://www.cnblogs.com/yixianyixian/p/7346894.html
2.SpringBootCondition
Condition的matches方法原生通过返回boolean值来进行条件判断,SpringBootCondition做了功能增强,在返回boolean值的基础上添加了日志信息的输出,ConditionOutcome类作为条件判断的结果输出,具体由子类来实现返回
@Override
public final boolean matches(ConditionContext context,
AnnotatedTypeMetadata metadata) {
String classOrMethodName = getClassOrMethodName(metadata);
ConditionOutcome outcome = getMatchOutcome(context, metadata);
logOutcome(classOrMethodName, outcome);
return outcome.isMatch();
}
public abstract ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata);
3.@ConditionalOnJava注解
以@ConditionalOnJava注解作为示例
@Configuration
@ConditionalOnJava(JavaVersion.EIGHT)
static class Java8Required {
@Bean
String foo() {
return "foo";
}
}
其内部实现的条件判断类是OnJavaCondition
protected ConditionOutcome getMatchOutcome(Range range, JavaVersion runningVersion,
JavaVersion version) {
boolean match = isWithin(runningVersion, range, version);
return new ConditionOutcome(match, "message");
}
private boolean isWithin(JavaVersion runningVersion, Range range,
JavaVersion version) {
if (range == Range.EQUAL_OR_NEWER) {
return runningVersion.isEqualOrNewerThan(version);
}
if (range == Range.OLDER_THAN) {
return runningVersion.isOlderThan(version);
}
}
这里先对SpringBootCondition的逻辑有一个了解