spring aop 通过获取代理对象实现事务切换
2016-12-08 19:36
330 查看
摘要:在项目中,当使用多数据源的时候,业务复杂的时候,会出现数据源A的方法里面嵌套调用数据源B的方法,这里面涉及到一个事务切换的问题,一般的方法没问题,根据通过aop注解在方法上通过加注解标识指定对应的数据源同时切换到对应的事务管理器,但是有这样的场景就不适用了,比如:数据源A的一个方法里面有调用数据源B的私有方法,这样的情况,如果还是通过方法上的注解就不行了,数据源B的方法也会走数据源A的事务管理器,这样的问题怎么解决呢,答案是:通过spring
aop类里面的AopContext类获取当前类的代理对象,这样就能切换对应的事务管理器了,具体做法如下:
(1).在applicationContext.xml文件中配置如下:
<!-- 开启暴露Aop代理到ThreadLocal支持 -->
<aop:aspectj-autoproxy expose-proxy="true"/>
(2).在需要切换的地方获取代理对象,再调用对应的方法,如下:
((PercentageRepository) AopContext.currentProxy()).findByPost(percentagePost);
(3).注意,这里需要被代理对象使用的方法必须是public类型的方法,不然获取不到代理对象,会报下面的错误:
java.lang.IllegalStateException: Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available.
(4).通过查看AopContext.class类的源代码得知,调用获取代理对象的方法必须是public修饰的,如下源代码:
public static Object currentProxy() throws IllegalStateException {
Object proxy = currentProxy.get();
if (proxy == null) {
throw new IllegalStateException(
"Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available.");
}
return proxy;
}
static Object setCurrentProxy(Object proxy) {
Object old = currentProxy.get();
if (proxy != null) {
currentProxy.set(proxy);
}
else {
currentProxy.remove();
}
return old;
}
参考地址:http://blog.csdn.net/huangjinlong77/article/details/42707571
aop类里面的AopContext类获取当前类的代理对象,这样就能切换对应的事务管理器了,具体做法如下:
(1).在applicationContext.xml文件中配置如下:
<!-- 开启暴露Aop代理到ThreadLocal支持 -->
<aop:aspectj-autoproxy expose-proxy="true"/>
(2).在需要切换的地方获取代理对象,再调用对应的方法,如下:
((PercentageRepository) AopContext.currentProxy()).findByPost(percentagePost);
(3).注意,这里需要被代理对象使用的方法必须是public类型的方法,不然获取不到代理对象,会报下面的错误:
java.lang.IllegalStateException: Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available.
(4).通过查看AopContext.class类的源代码得知,调用获取代理对象的方法必须是public修饰的,如下源代码:
public static Object currentProxy() throws IllegalStateException {
Object proxy = currentProxy.get();
if (proxy == null) {
throw new IllegalStateException(
"Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available.");
}
return proxy;
}
static Object setCurrentProxy(Object proxy) {
Object old = currentProxy.get();
if (proxy != null) {
currentProxy.set(proxy);
}
else {
currentProxy.remove();
}
return old;
}
参考地址:http://blog.csdn.net/huangjinlong77/article/details/42707571
相关文章推荐
- Spring aop 通过获取代理对象实现事务切换
- Spring aop 通过获取代理对象实现事务切换
- php使用curl通过代理获取数据的实现方法
- mybatis+spring中的Mapper通过非自动注入方式获取Mapper代理对象(Spring 工厂)
- php使用curl通过代理获取数据的实现方法
- 《Spring AOP学习总结之——通过动态代理实现AOP功能》
- 通过代理模式,对第三方网络请求框架进行封装,实现任意切换网络框架
- 通过反射获取代理对象的其目标对象
- SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论 SignalR 简单示例 通过三个DEMO学会SignalR的三种实现方式 SignalR推送框架两个项目永久连接通讯使用 SignalR 集线器简单实例2 用SignalR创建实时永久长连接异步网络应用程序
- 通过使用反射+动态代理+注解来实现对事务的控制
- Spring aop事务代理对象通过TransactionInterceptor处理目标方法事务过程,cglib方式
- 通过渲染到浮点纹理实现三维对象拾取
- 在.Net中通过序列化和反序列化实现对象实例化
- 通过代理为对象添加功能
- 通过js实现两个div鼠标滑过就切换
- ASP.NET 实现通过URL 获取远程的图片或者文件方法
- 通过WinInet API来实现表单提交并设置获取COOKIE
- Eclipse3.1.1+Webloigc 9.0通过数据源实现JDBC简单事务处理
- VB.NET下用FSO(文件系统对象模型)实现获取硬盘信息