Spring问题修复can not find proxy: set exposeproxy property on adv
一、前言
在项目启动后,页面点击时后台控制台出现如下错误提示,
can not find proxy: set exposeproxy property on advised to make it available
造成该问题原因是由于spring声明事务失效引起的。
二、实现方案
spring的aop无法拦截内部方法调用时,解决方案之一是重新获取代理类调用B方法。
下面说一下如何获取到代理bean。
- 
如果知道
beanName直接通过上下文获取到bean。 - 
如果不知道
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;
}
- 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)