Spring Boot指定JDK动态代理机制
将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