Spring进阶(六):can not find proxy: set exposeproxy property on advised to make it available

一、前言

在项目启动后,页面点击时后台控制台出现如下错误提示,

can not find proxy: set exposeproxy property on advised to make it available

造成该问题原因是由于spring声明事务失效引起的。

二、实现方案

springaop无法拦截内部方法调用时,解决方案之一是重新获取代理类调用B方法。

下面说一下如何获取到代理bean

  1. 如果知道beanName直接通过上下文获取到bean

  2. 如果不知道beanName,则可以放在线程变量中,在action中调用时可以先调用spring提供的接口AopContext.setCurrentProxy(proxy)。

接口原理就是将代理bean放到线程变量中。

public abstract class AopContext {
	private static final ThreadLocal<Object> currentProxy = new NamedThreadLocal<Object>("Current AOP proxy");
 
	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;
	}
 }

当需要调用时,则调用接口((BeanClass) AopContext.currentProxy()).B();

如果在配置中将expose-proxy设置为true,则直接获取就可以了:

<aop:config expose-proxy="true"><!—xml风格支持-->  

三、事务切换

接下来讲下spring aop通过获取代理对象实现事务切换。

在项目中,当使用多数据源的时候,业务复杂的时候,会出现数据源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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

No Silver Bullet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值