好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring aop 如何通过获取代理对象实现事务切换

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

查看更多关于Spring aop 如何通过获取代理对象实现事务切换的详细内容...

  阅读:26次