博客
关于我
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/

你可能感兴趣的文章
mysql每个数据库的最大连接数_MySQL数据库最大连接数
查看>>
Mysql流程控制结构,if函数、case结构、if结构、循环结构
查看>>
mysql添加外网访问权限
查看>>
mysql添加用户
查看>>
MySQL添加用户、删除用户与授权
查看>>
mysql添加用户及权限
查看>>
Mysql添加用户并授予只能查询权限
查看>>
mysql添加用户权限报1064 - You have an error in your SQL syntax问题解决
查看>>
mysql添加索引
查看>>
mysql添加表注释、字段注释、查看与修改注释
查看>>
mysql清理undo线程_MySQL后台线程的清理工作
查看>>
mysql清空带外键的表
查看>>
MySQL清空表数据
查看>>
mysql源码安装
查看>>
Mysql源码安装过程中可能碰到的问题
查看>>
MySQL灵魂16问,你能撑到第几问?
查看>>
MySQL灵魂拷问:36题带你面试通关
查看>>
mysql状态分析之show global status
查看>>
mysql状态查看 QPS/TPS/缓存命中率查看
查看>>
mysql生成树形数据_mysql 实现树形的遍历
查看>>