您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么獲得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 { /** * 獲取 目標對象 * @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代理對象中獲得原對象的類型 * @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; }
到此,關于“怎么獲得spring代理對象的原對象”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。