Spring项目中若开启CGLIB代理 spring.aop.proxy-target-class=true
注入接口后无法获取其实现类上注解。
通过debug得到class文件名含有EnhancerBySpringCGLIB:
com.xzh.service.impl.StrategyImplA$$EnhancerBySpringCGLIB$$f69341ff
clazz.getAnnotation();得到注解为空。
解决方案:
1.将spring.aop.proxy-target-class=true 去掉, 自动使用JDK代理。
2.使用注解解析器工具
import org.springframework.core.annotation.AnnotationUtils;
public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType) {
return findAnnotation(clazz, annotationType, true);
}