博客
关于我
cglib动态代理导致注解丢失问题及如何修改注解允许被继承
阅读量:429 次
发布时间: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/

你可能感兴趣的文章
netty的HelloWorld演示
查看>>
Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
查看>>
Netty的网络框架差点让我一夜秃头,哭了
查看>>
Netty相关
查看>>
Netty简介
查看>>
Netty线程模型理解
查看>>
netty解决tcp粘包和拆包问题
查看>>
Netty速成:基础+入门+中级+高级+源码架构+行业应用
查看>>
Netty遇到TCP发送缓冲区满了 写半包操作该如何处理
查看>>
netty(1):NIO 基础之三大组件和ByteBuffer
查看>>
Netty:ChannelPipeline和ChannelHandler为什么会鬼混在一起?
查看>>
Netty:原理架构解析
查看>>
Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
查看>>
Network Sniffer and Connection Analyzer
查看>>
Network 灰鸽宝典【目录】
查看>>
Networkx写入Shape文件
查看>>
NetworkX系列教程(11)-graph和其他数据格式转换
查看>>
Networkx读取军械调查-ITN综合传输网络?/读取GML文件
查看>>
network小学习
查看>>
Netwox网络工具使用详解
查看>>