博客
关于我
cglib动态代理导致注解丢失问题及如何修改注解允许被继承
阅读量:429 次
发布时间:2019-03-06

本文共 2558 字,大约阅读时间需要 8 分钟。

SOAService Bean注解丢失问题及解决方案

现象

SOAService bean通过两个BeanPostProcessor处理时,动态代理后发现bean上的注解@Service丢失。这一现象通常发生在开启CGLIB代理模式时。

为什么开启CGLIB代理

在Spring Boot应用中,@EnableAspectJAutoProxy(proxyTargetClass = true)注解用于启用AspectJ自动代理。proxyTargetClass = true的作用是将目标类直接作为代理类,这样在代理过程中会丢失原始类的注解信息,导致自定义注解无法保留。

如何解决这个问题

要解决@Service注解丢失的问题,可以采取以下方法:

  • 在自定义注解上添加@Inherited

    在自定义注解@Service上添加@Inherited注解,可以使得动态代理支持继承注解,从而保留注解信息。

  • 调整项目接口层

    对于第三方提供的接口,建议在项目接口层进行适当的调整,确保动态代理不会丢失重要的注解信息。

  • 动态修改注解以支持继承

    通过自定义BeanPostProcessor动态修改注解,使其支持继承。以下是具体实现代码:

  • 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注解支持继承            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;}

    通过上述方法,可以有效地解决动态代理后注解丢失的问题,确保自定义注解信息在代理类中得到保留。

    转载地址:http://xseuz.baihongyu.com/

    你可能感兴趣的文章
    python 启动提示IDLE's subprocess didn't make conne...
    查看>>
    Python 和 OpenCV.如何检测图像中的所有(填充)圆形/圆形对象?
    查看>>
    Python 和 RabbitMQ - 聆听来自多个渠道的消费事件的最佳方式?
    查看>>
    python编辑器打不开_关于命令ride.py打不开RF,而是打开pycharm编辑器问题解决思路...
    查看>>
    python编译exe同时支持32位_第三周:同时管理64位和32位版本的Python,并用Pyinstaller打包成exe...
    查看>>
    Python 和Java 哪个更适合做自动化测试?
    查看>>
    Python编程:掌握高级语言程序设计。从零基础到精通,收藏这篇就够了!
    查看>>
    python 图片转ico
    查看>>
    python 图片转文字、语音转文字、文字转语音保存音频并朗读
    查看>>
    python 在包含类似字符\x16、\x12、\x某某的数组中将以\x开头的字符找出来的方法
    查看>>
    Python 在并行进程之间共享字典
    查看>>
    Python 垃圾收集器文档
    查看>>
    python 基于 wordcloud + jieba + matplotlib 生成词云
    查看>>
    python 基于detectron或mask_rcnn的mask遮罩区域进行图片截取
    查看>>
    Python编程:Tkinter图形界面设计(2)
    查看>>
    Python 基础 - Day 5 Learning Note - 模块 之 标准库:time (1)
    查看>>
    Python 基础一
    查看>>
    Python 基础知识点整理与分享,期盼你的交流!
    查看>>
    python 基础第七篇
    查看>>
    Python编程:Tkinter图形界面设计(1)
    查看>>