博客
关于我
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基础语法:基本数据类型(列表)
    查看>>
    Python基础语法:内置函数
    查看>>
    Python基础语法:你不得不知的几种变量类型
    查看>>
    Python基础语法教程,零基础入门到精通,看完这一篇就够了
    查看>>
    Python基础语法与执行脚本的3种方式
    查看>>
    python基础语法
    查看>>
    python基础语法
    查看>>
    python系列【仅供参考】:python测试开发基础---multiprocessing.Pool
    查看>>
    Python基础综合练习
    查看>>
    Python基础练习之一输出10000以内的阿姆斯特朗数
    查看>>
    Python基础系列讲解—动态类型语言的特点
    查看>>
    python基础系列四:字符串,列表,字典,元组之间的转换
    查看>>
    python基础篇(9):模块
    查看>>
    python基础篇(8):异常处理
    查看>>
    python基础篇(8):字符串常用处理汇总
    查看>>
    python基础篇(7):函数进阶知识点
    查看>>
    python基础篇(6):global关键字
    查看>>
    python基础篇(5):None类型
    查看>>
    python基础篇(4):range语句
    查看>>
    python基础篇(3):print()补偿知识点
    查看>>