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);
}
本文探讨了Spring项目中CGLIB代理与JDK代理在处理实现类注解上的区别,当spring.aop.proxy-target-class设为true时,CGLIB代理下无法直接获取到接口实现类上的注解,提供了两种解决方案:一是关闭CGLIB代理,二是使用AnnotationUtils工具类。
2184

被折叠的 条评论
为什么被折叠?



