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

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

现象

  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);    }}

为什么开启这个代理模式呢

   

如何解决这个问题

  在自定义注解上添加@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;}

 

  参考链接:

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

你可能感兴趣的文章
wait()与notify()
查看>>
使用js打印时去除页眉页脚
查看>>
Spring security OAuth2.0认证授权学习第二天(基础概念-RBAC)
查看>>
ORA-00904: "FILED_TYPE": 标识符无效
查看>>
数据仓库系列之维度建模
查看>>
Scala教程之:函数式的Scala
查看>>
java中DelayQueue的使用
查看>>
线程stop和Interrupt
查看>>
Android中定时执行任务的3种实现方法
查看>>
nodejs中npm常用命令
查看>>
mybatis一个怪异的问题: Invalid bound statement not found
查看>>
基于Vue2.0+Vue-router构建一个简单的单页应用
查看>>
基于vue2.0实现仿百度前端分页效果(二)
查看>>
JS魔法堂:函数重载 之 获取变量的数据类型
查看>>
时间序列神器之争:Prophet VS LSTM
查看>>
SpringBoot中关于Mybatis使用的三个问题
查看>>
MapReduce实验
查看>>
Leaflet 带箭头轨迹以及沿轨迹带方向的动态marker
查看>>
java大数据最全课程学习笔记(1)--Hadoop简介和安装及伪分布式
查看>>
java大数据最全课程学习笔记(2)--Hadoop完全分布式运行模式
查看>>