如何获得spring代理对象的原对象
更新时间:2021年07月19日 12:05:44 作者:单曲TI
这篇文章主要介绍了如何获得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 { /** * 获取 目标对象 * @param proxy 代理对象 * @return * @throws Exception */ 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代理的对象中获取源对象
/** * 从AOP代理对象中获得原对象的类型 * @param proxyObject * @return */ @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; }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
springmvc无法访问/WEB-INF/views下的jsp的解决方法
本篇文章主要介绍了springmvc无法访问/WEB-INF/views下的jsp的解决方法,非常具有实用价值,需要的朋友可以参考下2017-10-10初学者易上手的SSH-struts2 01环境搭建(图文教程)
下面小编就为大家带来一篇初学者易上手的SSH-struts2 01环境搭建(图文教程)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-10-10slf4j与jul、log4j1、log4j2、logback的集成原理
这篇文章主要介绍了slf4j与jul、log4j1、log4j2、logback的集成原理,以及通用日志框架与具体日志实现系统的机制机制介绍,包括依赖的jar包,jar冲突处理等2022-03-03以Spring Boot的方式显示图片或下载文件到浏览器的示例代码
这篇文章主要介绍了以Spring Boot的方式显示图片或下载文件到浏览器的示例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-01-01
最新评论