昨天有一个小伙伴遇到一个问题。他最开始是通过在类上面添加注解(这个类纳入了 Spring 容器的管理),然后再通过继承 Spring 容器的扩展类 ApplicationObjectSupport 找到 Spring 容器中所有标注了之前提到的注解。后面解析这个注解,然后把这个注解标注的唯一值和当前对象实例放到一个 MAP 当中。通过这样的方式就实现了策略模式,通过注解上面的标注唯一的值到 ApplicationObjectSupport 的继承类里面获取需要的实例。
上面就是出现问题的前提,后面在策略类上面添加了切面这个类就不能获取到标注在上面的注解了。这样也导致他不能添加到策略 Map 当中去了。然后我看了一下他的代码实现:
解析标注注解
Class clazz = bean.getClass(); 标注注解 annotation = clazz.getAnnotation(标注注解.class);
当前遇到这个问题的时候我直接把代码替换为:
备注:我之前也没有遇到过这种情况
Class clazz = bean.getClass(); 标注注解 annotation = AnnotationUtils.findAnnotation(clazz, 标注注解.class);
然后叫他一尝试,问题解决了。之前我在看源码的时候看到过这个类,当时他遇到这个问题的时候。我就想 Spring 既然提供了注解相关的工具类它考虑我们在代码中遇到的场景肯定比较多。结果还真解决了。
首先我们来分析一下问题出现之前以及问题出现之后的不同点:
- 问题出现之前:这个是一个普通的 Spring bean
- 问题出现之后:这个是一个代理的 Spring bean
而 bean.getClass() 这个方法是获取当前对象的 Class 对象。当对象被 Spring 代理了,不管是 JDK 的动态还是 CGLIB 的动态代理。都是基于继承的方式来实现的,所以上面获取的 Class 对象是定义的 Spring bean 的子类。
下面我们来看一下 AnnotationUtils 的实现:
AnnotationUtils#findAnnotation(java.lang.reflect.AnnotatedElement, java.lang.Class)
public static A findAnnotation( AnnotatedElement annotatedElement, @Nullable Class annotationType) { if (annotationType == null) { return null; } // #1 // Shortcut: directly present on the element, with no merging needed? if (AnnotationFilter.PLAIN.matches(annotationType) || AnnotationsScanner.hasPlainJavaAnnotationsOnly(annotatedElement)) { return annotatedElement.getDeclaredAnnotation(annotationType); } // #2 // Exhaustive retrieval of merged annotations... return MergedAnnotations.from(annotatedElement, SearchStrategy.INHERITED_ANNOTATIONS, RepeatableContainers.none()) .get(annotationType).withNonMergedAttributes() .synthesize(MergedAnnotation::isPresent).orElse(null); }
- #1 的逻辑是:如果满足 AnnotationFilter.PLAIN 在 java.lang 或者 org.springframework.lang 或者这个 java.lang.reflect.Classs或者 java.lang.reflect.Member 本身直接在当前 Class 中获取声明的注解信息。
- #2 的逻辑是:创建一个 MergedAnnotations 的子类 TypeMappedAnnotations,用于这个对象提供对合并注释集合的访问,它的搜索策略是 SearchStrategy.INHERITED_ANNOTATIONS。也就是查询当前类及其所有的父类中的注解(接口除外)。
在上面已经构建好了 Class 中所有的元数据访问对象 TypeMappedAnnotations,下面就是直接从这个对象里面获取给定的注解信息。调用的入口是 TypeMappedAnnotations#get。下面我们来看一下它的调用时序图:
最后来到它的核心方法 AnnotationsScanner#processClassInheritedAnnotations,逻辑如下:
AnnotationsScanner#processClassInheritedAnnotations
private staticR processClassInheritedAnnotations(C context, Class> source, SearchStrategy searchStrategy, AnnotationsProcessor processor, @Nullable BiPredicate > classFilter) { try { // #1 if (isWithoutHierarchy(source, searchStrategy)) { return processElement(context, source, processor, classFilter); } Annotation[] relevant = null; int remaining = Integer.MAX_VALUE; int aggregateIndex = 0; Class> root = source; while (source != null && source != Object.class && remaining > 0 && !hasPlainJavaAnnotationsOnly(source)) { // #2 R result = processor.doWithAggregate(context, aggregateIndex); if (result != null) { return result; } // #3 if (isFiltered(source, context, classFilter)) { continue; } // #4 Annotation[] declaredAnnotations = getDeclaredAnnotations(context, source, classFilter, true); if (relevant == null && declaredAnnotations.length > 0) { relevant = root.getAnnotations(); remaining = relevant.length; } for (int i = 0; i < declaredAnnotations.length; i++) { if (declaredAnnotations[i] != null) { boolean isRelevant = false; for (int relevantIndex = 0; relevantIndex < relevant.length; relevantIndex++) { if (relevant[relevantIndex] != null && declaredAnnotations[i].annotationType() == relevant[relevantIndex].annotationType()) { isRelevant = true; relevant[relevantIndex] = null; remaining--; break; } } if (!isRelevant) { declaredAnnotations[i] = null; } } } result = processor.doWithAnnotations(context, aggregateIndex, source, declaredAnnotations); if (result != null) { return result; } source = source.getSuperclass(); aggregateIndex++; } } catch (Throwable ex) { AnnotationUtils.handleIntrospectionFailure(source, ex); } return null; }
下面我们来分析一下这里面的核心逻辑:
- #1 的逻辑:如果是反射中的 Class、 Method 或者直接是 Object 对象,直接获取这个 Class 上面的注解
- #2 的逻辑:如果直接有结果,直接返回。
- #3 的逻辑:满足过滤条件则直接跳过,从外面传过来的 ClassFilter 为空。
- #4 的逻辑:查询到当前类及其父类的所有注解直到找到匹配的注解直到对象是 Object
代理对象其实是对我们标注注解的类的一个子类,所以通过上面的方法就可以搜索到我们标注的注解信息。



