cglib动态代理导致注解丢失问题及如何修改注解允许被继承
2018-02-09 00:56
531 查看
现象
SOAService这个bean先后经过两个BeanPostProcessor,会发现代理之后注解就丢失了。开启了cglib代理
@SpringBootApplication @EnableAspectJAutoProxy(proxyTargetClass = true) public class Application { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application.class); app.run(args); } }
为什么开启这个代理模式呢
http://www.cnblogs.com/hujunzheng/p/8428422.html如何解决这个问题
在自定义注解上添加@Inherited。如果是第三方的注解,调整项目接口层或者拿到这个注解通过代码方式加上@Inherited注解, 或者如下图所示。public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { Service anon = bean.getClass().getAnnotation(Service.class); if (anon != null) { try { InvocationHandler h = Proxy.getInvocationHandler(anon); //设置@Service注解支持继承,应对动态代理导致类上的@Service注解丢失 Field typeField = h.getClass().getDeclaredField("type"); typeField.setAccessible(true); Field annotationTypeField = Class.class.getDeclaredField("annotationType"); annotationTypeField.setAccessible(true); AnnotationType annotationType = (AnnotationType) annotationTypeField.get(typeField.get(h)); Field inheritedField = AnnotationType.class.getDeclaredField("inherited"); this.updateFinalModifiers(inheritedField); inheritedField.set(annotationType, true); // 获取 AnnotationInvocationHandler 的 memberValues 字段 Field memberValuesField = h.getClass().getDeclaredField("memberValues"); // 因为这个字段事 private final 修饰,所以要打开权限 memberValuesField.setAccessible(true); // 获取 memberValues Map memberValues = (Map) memberValuesField.get(h); Service service = Stream.of(bean.getClass().getInterfaces()) .filter(iface -> iface.getAnnotation(Service.class) != null) .collect(Collectors.toList()) .get(0) .getAnnotation(Service.class); memberValues.put("version", service.version()); memberValues.put("group", service.group()); } catch (Exception e) { throw new BeanCreationException(String.format("%s %s %s %s %s" , "修改" , ClassUtils.getQualifiedName(bean.getClass()) , "的注解" , ClassUtils.getQualifiedName(Service.class) , "的 group值和version值出错") , e); } } return bean; }
参考链接:Annotation和动态代理
相关文章推荐
- spring02 注解方式实现MVC、spring的继承、代理模式(静/动) :jdk动态代理,cglib动态代理
- 关于mysql 修改权限表user字段host导致权限丢失的问题
- 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。
- 增强 修改对象的集中方法(继承、装饰者模式、动态代理)
- weblogic通过代理插件weblogic.servlet.proxy.HttpProxyServlet跨域访问导致session丢失问题的解决
- 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务
- ef codeFirst 修改表结构 增加字段等 EF code first需要重新生成库导致数据丢失的问题.
- 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别
- 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。
- Cglib实现动态代理-解决大对象值传递问题
- spring 如何决定使用jdk动态代理和cglib(网易面试题)
- 如何拿回被恶意或者不小心删除硬盘所有者权限而导致的文件访问以及修改权限被限制(NTFS格式安全权限问题)
- 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别
- GridView动态添加列之后,导致PostBack(回发)页面数据丢失问题解决
- 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。
- 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务
- 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。
- 解决Proxy.newProxyInstance创建动态代理导致类型转换错误的问题
- 在Spring中,是如何实现 Aop 的,原理:动态代理+cglib 分步图解
- 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。