SpringBoot注解@ConditionalOnClass底層源碼實現(xiàn)
@ConditionalOnClass的底層源碼實現(xiàn)
在SpringBoot中,支持了很多種條件注解,@ConditionalOnClass注解就是其中之一,而且及其重要,它主要是用來判斷該注解所指定的某個類或某些類,是否在ClassPath中存在,如果存在則符合條件,如果不存在則不符合。
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnClass {
Class<?>[] value() default {};
String[] name() default {};
}
這是該注解的源碼,可以通過value和name來指定要判斷的類,而真正執(zhí)行判斷的邏輯在OnClassCondition類中。
- OnClassCondition類繼承了FilteringSpringBootCondition類
- FilteringSpringBootCondition類又繼承了SpringBootCondition類
- SpringBootCondition類實現(xiàn)了Condition接口

Spring在解析條件注解時,就會調(diào)用Condition接口的matches()方法,在上面的類繼承關系中,SpringBootCondition類實現(xiàn)了matches()方法,所以會先被調(diào)用。
ConditionOutcome對象
在matches()方法中,會調(diào)用getMatchOutcome()方法,并得到ConditionOutcome對象,ConditionOutcome對象就表示條件判斷的結果。
public class ConditionOutcome {
// 表示條件是否匹配
private final boolean match;
// ...
}
getMatchOutcome()方法在SpringBootCondition類中是一個抽象方法,在子類OnClassCondition類中才真正實現(xiàn)了getMatchOutcome()方法,并真正會進行條件判斷。
所以核心就是這個getMatchOutcome()方法,在這個方法中會先獲取@ConditionalOnClass注解的value和name屬性的值,這些值就是待判斷的類名集合。
// 調(diào)用getCandidates方法 List<String> onClasses = getCandidates(metadata, ConditionalOnClass.class);
private List<String> getCandidates(AnnotatedTypeMetadata metadata, Class<?> annotationType) {
MultiValueMap<String, Object> attributes =
metadata.getAllAnnotationAttributes(annotationType.getName(), true);
if (attributes == null) {
return null;
}
List<String> candidates = new ArrayList<>();
addAll(candidates, attributes.get("value"));
addAll(candidates, attributes.get("name"));
return candidates;
}
ClassNameFilter.MISSING判斷某類是否不存在
接下來就會逐個判斷類名集合中的每個類名,判斷邏輯為:利用ClassNameFilter.MISSING來判斷某類是否不存在?
List<String> missing = filter(onClasses, ClassNameFilter.MISSING, classLoader);
protected final List<String> filter(Collection<String> classNames, ClassNameFilter classNameFilter, ClassLoader classLoader) {
if (CollectionUtils.isEmpty(classNames)) {
return Collections.emptyList();
}
List<String> matches = new ArrayList<>(classNames.size());
for (String candidate : classNames) {
if (classNameFilter.matches(candidate, classLoader)) {
matches.add(candidate);
}
}
return matches;
}
ClassNameFilter.MISSING就是利用ClassLoader來加載類,如果加載到了表示類存在,沒加載到就表示不存在。
protected enum ClassNameFilter {
// ...
MISSING {
@Override
public boolean matches(String className, ClassLoader classLoader) {
// 是否不存在
return !isPresent(className, classLoader);
}
};
static boolean isPresent(String className, ClassLoader classLoader) {
if (classLoader == null) {
classLoader = ClassUtils.getDefaultClassLoader();
}
try {
resolve(className, classLoader);
return true;
}
catch (Throwable ex) {
return false;
}
}
}
protected static Class<?> resolve(String className, ClassLoader classLoader) throws ClassNotFoundException {
if (classLoader != null) {
return Class.forName(className, false, classLoader);
}
return Class.forName(className);
}
判斷完之后,只要missing集合不為空,那就表示待判斷的類中有類不存在,那就返回條件不匹配的ConditionOutcome對象,否則就返回條件匹配的ConditionOutcome對象。
這就是@ConditionalOnClass注解的核心源碼流程,期待你的點贊哦。
以上就是SpringBoot注解@ConditionalOnClass底層源碼實現(xiàn)的詳細內(nèi)容,更多關于SpringBoot ConditionalOnClass的資料請關注腳本之家其它相關文章!
相關文章
logback?OutputStreamAppender高效日志輸出源碼解析
這篇文章主要介紹了為大家logback?OutputStreamAppender日志輸出效率提升示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報錯問題
這篇文章主要介紹了MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報錯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06

