久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

Spring aop 如何通過獲取代理對象實現事務切換

瀏覽:68日期:2023-06-30 16:50:00
Spring aop 獲取代理對象實現事務切換

在項目中,涉及到同一個類中一個方法調用另外一個方法,并且兩個方法的事務不相關,

這里面涉及到一個事務切換的問題,一般的方法沒問題,根據通過aop注解在方法上通過加注解標識,

答案是:

通過spring aop類里面的AopContext類獲取當前類的代理對象,

這樣就能切換對應的事務管理器了,具體做法如下:

(1).在applicationContext.xml文件中配置如下:

<!-- 開啟暴露Aop代理到ThreadLocal支持 --> <aop:aspectj-autoproxy expose-proxy='true'/> (2).在需要切換的地方獲取代理對象,

再調用對應的方法,如下:

((類名) AopContext.currentProxy()).方法(); (3).注意

這里需要被代理對象使用的方法必須是public類型的方法,不然獲取不到代理對象,會報下面的錯誤:

java.lang.IllegalStateException: Cannot find current proxy: Set ’exposeProxy’ property on Advised to ’true’ to make it available.

開啟暴露AOP代理即可.

因為開啟事務和事務回滾,實際這個過程是aop代理幫忙完成的,當調用一個方法時,它會先檢查時候有事務,有則開啟事務,

當調用本類的方法是,它并沒有將其視為proxy調用,而是方法的直接調用,所以也就沒有檢查該方法是否含有事務這個過程,

那么本地方法調用的事務也就無效了。

獲取代理bean的原始對象

public class AopTargetUtil { /** * 獲取 目標對象 * @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; }}

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 图木舒克市| 颍上县| 莆田市| 湖口县| 石屏县| 新建县| 曲松县| 日喀则市| 海盐县| 张家界市| 小金县| 巩留县| 民勤县| 罗甸县| 郴州市| 通州区| 玛曲县| 年辖:市辖区| 民权县| 凤山市| 阳谷县| 微山县| 太谷县| 英超| 治县。| 甘谷县| 张家口市| 赫章县| 当涂县| 上栗县| 潍坊市| 开化县| 宕昌县| 平邑县| 漳平市| 临泉县| 铜山县| 桃源县| 辽中县| 河间市| 乐亭县|