怎么获得spring代理对象的原对象
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍“怎么获得spring代理对象的原对象”,在日常操作中,相信很多人在怎么获得spring代理对象的原对象问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么获得spring代理对象的原对象”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
获得spring代理对象的原对象
看代码吧:
@SuppressWarnings({"unchecked"})protected <T> T getTargetObject(Object proxy, Class<T> targetClass) throws Exception { if (AopUtils.isJdkDynamicProxy(proxy)) { return (T) ((Advised)proxy).getTargetSource().getTarget(); } else { return (T) proxy; // expected to be cglib proxy then, which is simply a specialized class }}
public class AopTargetUtils { public static Object getTarget(Object proxy) throws Exception { if(!AopUtils.isAopProxy(proxy)) { return proxy;//不是代理对象 } if(AopUtils.isJdkDynamicProxy(proxy)) { return getJdkDynamicProxyTargetObject(proxy); } else { //cglib return getCglibProxyTargetObject(proxy); } } private static Object getCglibProxyTargetObject(Object proxy) throws Exception { Field h = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0"); h.setAccessible(true); Object dynamicAdvisedInterceptor = h.get(proxy); Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised"); advised.setAccessible(true); Object target = ((AdvisedSupport)advised.get(dynamicAdvisedInterceptor)).getTargetSource().getTarget(); return target; } private 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; } @SuppressWarnings({"unchecked"}) protected <T> T getTargetObject(Object proxy, Class<T> targetClass) throws Exception { if (AopUtils.isJdkDynamicProxy(proxy)) { return (T) ((Advised)proxy).getTargetSource().getTarget(); } else { return (T) proxy; // expected to be cglib proxy then, which is simply a specialized class } }}
从Aop代理的对象中获取源对象
@SuppressWarnings("unchecked") public static final <T> T getBeanFromProxy(T proxyObject) { Class<?> clazz = proxyObject.getClass(); if(clazz.getName().startsWith("$Proxy")) { try { clazz = clazz.getSuperclass(); Field hField = clazz.getDeclaredField("h"); hField.setAccessible(true); Object hObject = hField.get(proxyObject); Class<?> dynamicProxyClass = hObject.getClass(); Field advisedField = dynamicProxyClass.getDeclaredField("advised"); advisedField.setAccessible(true); Object advisedObject = advisedField.get(hObject); Class<?> advisedSupportClass = advisedObject.getClass().getSuperclass().getSuperclass(); Field targetField = advisedSupportClass.getDeclaredField("targetSource"); targetField.setAccessible(true); Object targetObject = targetField.get(advisedObject); Class<?> targetSourceClass = targetObject.getClass(); Field targetClassField = targetSourceClass.getDeclaredField("target"); targetClassField.setAccessible(true); return (T) targetClassField.get(targetObject); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return null; }
到此,关于“怎么获得spring代理对象的原对象”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341