2 回答

TA貢獻1942條經驗 獲得超3個贊
事實證明,有一個實用程序集AnnotatedElementUtils可以讓您處理那些合并的注釋。
for (Object annotated : context.getBeansWithAnnotation(ComponentScan.class).values()) {
Class clazz = ClassUtils.getUserClass(annotated) // thank you jin!
ComponentScan mergedAnnotation = AnnotatedElementUtils.getMergedAnnotation(clazz, ComponentScan.class);
if (mergedAnnotation != null) { // For some reasons, this might still be null.
// TODO: useful stuff.
}
}

TA貢獻1909條經驗 獲得超7個贊
它可能是 CglibProxy。所以不能直接獲取Annotation。
ClassUtils.isCglibProxyClass(o)
有關更多信息,請參閱此
編輯,你可以添加你的邏輯代碼。找到 ComponentScan。
if (ClassUtils.isCglibProxyClass(o.getClass())) {
Annotation[] annotations = ClassUtils.getUserClass(o).getAnnotations();
for (Annotation annotation : annotations) {
ComponentScan annotation1 = annotation.annotationType().getAnnotation(ComponentScan.class);
// in my test code , ComponentScan can get here.for @SpringBootApplication
System.out.println(annotation1);
}
}
添加回答
舉報