Spring Boot指定JDK动态代理机制

时间:2020-03-18 18:05:57   收藏:0   阅读:196

将1.5.9.RELEASE版本的Spring Boot 项目添加 Spring Cloud依赖后,以下方法报错

    public static Object getJdkDynamicProxyTargetObject(Object proxy) throws Exception {
        Field h = proxy.getClass().getSuperclass().getDeclaredField("h");
        h.setAccessible(true);
        AopProxy aopProxy = (AopProxy) h.get(proxy);

        Field advised = aopProxy.getClass().getDeclaredField("advised");
        advised.setAccessible(true);

        Object target = ((AdvisedSupport)advised.get(aopProxy)).getTargetSource().getTarget();

        return target;
    }

该方法是获取jdk动态代理机制的目标对象。引入Spring Cloud依赖后,Spring Boot 自动切换为cglib机制了,此方法不能获取到目标对象。
解决方案为:强制指定Spring Boot的代理机制为jdk动态代理,在yml文件中,增加以下配置

spring:
    aop:
      proxy-target-class: false

原文:https://www.cnblogs.com/richardz/p/12518147.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!