Spring aop 獲取代理對象實現事務切換
在項目中,涉及到同一個類中一個方法調用另外一個方法,并且兩個方法的事務不相關,
這里面涉及到一個事務切換的問題,一般的方法沒問題,根據通過aop注解在方法上通過加注解標識,
答案是:
通過spring aop類里面的AopContext類獲取當前類的代理對象,
這樣就能切換對應的事務管理器了,具體做法如下:
(1).在applicationContext.xml文件中配置如下:
1
2
|
<!-- 開啟暴露Aop代理到ThreadLocal支持 --> < aop:aspectj-autoproxy expose-proxy = "true" /> |
(2).在需要切換的地方獲取代理對象,
再調用對應的方法,如下:
1
|
((類名) 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的原始對象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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; } } |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq496013218/article/details/77102164