博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ProxyConfig属性之exposeProxy
阅读量:2155 次
发布时间:2019-05-01

本文共 2366 字,大约阅读时间需要 7 分钟。

exposeProxy

((Pojo) AopContext.currentProxy()).bar();

如果调用了AopContext.currentProxy,需要把exposeProxy设置true,否则会报下面错误:

Exception in thread "main" java.lang.IllegalStateException: Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available.

JdkDynamicAopProxy

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {    if (this.advised.exposeProxy) {  //只有true的时候,才设置AopContext.setCurrentProx	    // Make invocation available if necessary.	    oldProxy = AopContext.setCurrentProxy(proxy);	    setProxyContext = true;    }}

 

AopContext

AopContext 其实就是1个ThreadLocal,通过当前线程得到代理类,前提是currentProxy要设置true

public final class AopContext {	/**	 * ThreadLocal holder for AOP proxy associated with this thread.	 * Will contain {@code null} unless the "exposeProxy" property on	 * the controlling proxy configuration has been set to "true".	 * @see ProxyConfig#setExposeProxy	 */	private static final ThreadLocal currentProxy = new NamedThreadLocal<>("Current AOP proxy");	private AopContext() {	}	/**	 * Try to return the current AOP proxy. This method is usable only if the	 * calling method has been invoked via AOP, and the AOP framework has been set	 * to expose proxies. Otherwise, this method will throw an IllegalStateException.	 * @return the current AOP proxy (never returns {@code null})	 * @throws IllegalStateException if the proxy cannot be found, because the	 * method was invoked outside an AOP invocation context, or because the	 * AOP framework has not been configured to expose the 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;	}	/**	 * Make the given proxy available via the {@code currentProxy()} method.	 * 

Note that the caller should be careful to keep the old value as appropriate. * @param proxy the proxy to expose (or {@code null} to reset it) * @return the old proxy, which may be {@code null} if none was bound * @see #currentProxy() */ @Nullable static Object setCurrentProxy(@Nullable Object proxy) { Object old = currentProxy.get(); if (proxy != null) { currentProxy.set(proxy); } else { currentProxy.remove(); } return old; }}

 

 

 

 

 

 

 

 

转载地址:http://lqxwb.baihongyu.com/

你可能感兴趣的文章
JavaScript 经典例子
查看>>
判断数据的JS代码
查看>>
js按键事件说明
查看>>
AJAX 初次体验!推荐刚学看这个满好的!
查看>>
AJAX 设计制作 在公司弄的 非得要做出这个养的 真晕!
查看>>
Linux 查看文件大小
查看>>
Java并发编程:线程池的使用
查看>>
redis单机及其集群的搭建
查看>>
Java多线程学习
查看>>
检查Linux服务器性能
查看>>
Java 8新的时间日期库
查看>>
Chrome开发者工具
查看>>
【LEETCODE】102-Binary Tree Level Order Traversal
查看>>
【LEETCODE】106-Construct Binary Tree from Inorder and Postorder Traversal
查看>>
【LEETCODE】202-Happy Number
查看>>
和机器学习和计算机视觉相关的数学
查看>>
十个值得一试的开源深度学习框架
查看>>
【LEETCODE】240-Search a 2D Matrix II
查看>>
【LEETCODE】53-Maximum Subarray
查看>>
【LEETCODE】215-Kth Largest Element in an Array
查看>>