写在前面

  expose-proxy。为是否暴露当前代理对象为ThreadLocal模式。

  SpringAOP对于最外层的函数只拦截public方法,不拦截protected和private方法(后续讲解),另外不会对最外层的public方法内部调用的其他方法也进行拦截,即只停留于代理对象所调用的方法。

案例分析

public class AServiceImpl implements AService{
@Override
public void barA() {
System.out.println("AServiceImpl.barA()");
barB();
}
@Override
public void barB() {
System.out.println("AServiceImpl.barB()");
}
}

控制台的输出结果:

run my before advice
AServiceImpl.barA()
AServiceImpl.barB()

分析:

  发现aop并没有对barB方法进行增强,只是增强了barA方法。

  判断上述this.barB()方法是否被拦截的最本质的东西是看this到底是谁?有如下对象B类的对象b,和cglib生成的代理对象bProxy,代理对象bProxy内部拥有b。如果调用b对象的任何方法,肯定不会发生任何拦截,当调用bProxy的方法则都会进入拦截函数。

  当我们调用bProxy对象的barA()方法时,先执行cglib之前设置的callback对象的intercept拦截函数,如下: (之前的文章已经分析过代码)

public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
Object oldProxy = null;
boolean setProxyContext = false;
Class<?> targetClass = null;
Object target = null;
try {
if (this.advised.exposeProxy) {
oldProxy = AopContext.setCurrentProxy(proxy);
setProxyContext = true;
}
target = getTarget();
if (target != null) {
targetClass = target.getClass();
}
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
Object retVal;
if (chain.isEmpty() && Modifier.isPublic(method.getModifiers())) {
retVal = methodProxy.invoke(target, args);
}
else {
retVal = new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed();
}
retVal = processReturnType(proxy, target, method, retVal);
return retVal;
}
finally {
if (target != null) {
releaseTarget(target);
}
if (setProxyContext) {
AopContext.setCurrentProxy(oldProxy);
}
}
}

  这个过程之前的文章已经分析过,这里就是首先取出拦截器链List<Object> chain,当barA方法不符合我们所配置的pointcut时,拦截器链必然为空,然后就是直接执行目标对象的方法。 
当barA方法符合所配置的pointcut时,拦截器链不为空,执行相应的通知advice,currentInterceptorIndex 从-1开始,如下(之前的文章已经分析过代码)

public Object proceed() throws Throwable {
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
return invokeJoinpoint();
}
Object interceptorOrInterceptionAdvice =
this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
InterceptorAndDynamicMethodMatcher dm =
(InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {
return dm.interceptor.invoke(this);
}
else {
return proceed();
}
}
else {
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
}
}

  随着通知不断的传递执行,最终this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1将会满足条件,将会来到执行目标对象的方法invokeJoinpoint():

protected Object invokeJoinpoint() throws Throwable {
if (this.publicMethod) {
return this.methodProxy.invoke(this.target, this.arguments);
}
else {
return super.invokeJoinpoint();
}
}

  在这里不管要拦截的目标方法是不是public方法,最终所传递的对象都是this.target,他是目标对象而不是代理对象,即执行上述barA()函数的对象是目标对象而不是代理对象,所以它内部所调用的this.barB()也是目标对象,因此不会发生拦截,如果是执行的是代理对象.barB()则必然会进入intercept拦截过程。所以上述调用barA函数,其内部调用的barB函数是不会发生拦截的,因为this指的是目标对象,不是代理对象。

实现BarB拦截

  如果你想实现barA调用时内部的BarB也进行拦截,就必须把this换成代理对象。这时就要用到了,xml配置中的expose-proxy="true",即暴露出代理对象,它使用的是ThreadLocal设计模式,我们可以这样获取代理对象,AopContext.currentProxy()就是代理对象,然后转换成目标对象或者目标接口,执行相应的方法:

public class AServiceImpl implements AService{
@Override
public void barA() {
System.out.println("AServiceImpl.barA()");
AService proxy=(AService) AopContext.currentProxy();
proxy.barB();
}
@Override
public void barB() {
System.out.println("AServiceImpl.barB()");
}
}
    <bean id="aServiceImplProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="exposeProxy">
<value>true</value>
</property>
<property name="interfaces" value="com.xxx.plus.aop.demo.AService"/>
<property name="target">
<ref bean="aServiceImpl"/>
</property>
<property name="interceptorNames">
<list>
<value>myBeforAdvice</value>
</list>
</property>
</bean>

执行结果:

run my before advice
AServiceImpl.barA()
run my before advice
AServiceImpl.barB()

分析:

  barA()和barB都被aop拦截实现了增强

最后再给出Spring的文档说明:

  Due to the proxy-based nature of Spring’s AOP framework, protected methods are by definition not intercepted, neither for JDK proxies (where this isn’t applicable) nor for CGLIB proxies (where this is technically possible but not recommendable for AOP purposes). As a consequence, any given pointcut will be matched against public methods only! 
  If your interception needs include protected/private methods or even constructors, consider the use of Spring-driven native AspectJ weaving instead of Spring’s proxy-based AOP framework. This constitutes a different mode of AOP usage with different characteristics, so be sure to make yourself familiar with weaving first before making a decision.

转自:https://www.cnblogs.com/chihirotan/p/7356683.html

Spring AOP expose-proxy的更多相关文章

  1. 阿里四面:你知道Spring AOP创建Proxy的过程吗?

    Spring在程序运行期,就能帮助我们把切面中的代码织入Bean的方法内,让开发者能无感知地在容器对象方法前后随心添加相应处理逻辑,所以AOP其实就是个代理模式. 但凡是代理,由于代码不可直接阅读,也 ...

  2. Spring AOP 的proxy详解

    spring 提供了多种不同的方案实现对 bean 的 aop proxy, 包括 ProxyFactoryBean, 便利的 TransactionProxyFactoryBean 以及 AutoP ...

  3. spring Aop设计原理

    转载至:https://blog.csdn.net/luanlouis/article/details/51095702 0.前言 Spring 提供了AOP(Aspect Oriented Prog ...

  4. Spring aop报错:com.sun.proxy.$Proxyxxx cannot be cast to yyy

    在使用Spring AOP时,遇到如下的错误: Exception in thread "main" java.lang.ClassCastException: com.sun.p ...

  5. Spring AOP的实现研究

    1. 背景 在前文Spring IOC容器创建bean过程浅析已经介绍了Spring IOC创建初始化bean的大致过程.现在对Spring的AOP实现机制进行研究分析. 2. 名词与概念 名词 概念 ...

  6. Spring AOP源码分析(三)创建AOP代理

    摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 目录 一.获取增强器 1. 普通增强器的获取 2. 增加同步实例化增强 ...

  7. Spring技术内幕:Spring AOP的实现原理(三)

    生成SingleTon代理对象在getSingleTonInstance方法中完毕,这种方法时ProxyFactoryBean生成AopProxy对象的入口.代理对象会封装对target目标对象的调用 ...

  8. Spring AOP学习笔记04:AOP核心实现之创建代理

    上文中,我们分析了对所有增强器的获取以及获取匹配的增强器,在本文中我们就来分析一下Spring AOP中另一部分核心逻辑--代理的创建.这部分逻辑的入口是在wrapIfNecessary()方法中紧接 ...

  9. Spring AOP底层实现分析

    Spring AOP代理对象的生成 Spring提供了两种方式来生成代理对象: JdkProxy和Cglib,具体使用哪种方式生成由AopProxyFactory根据AdvisedSupport对象的 ...

  10. 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

随机推荐

  1. Jmeter之Synchronizing Timer(同步集合点)

    在性能测试时,需要压测并发,此时就需要用到Synchronizing Timer组件. 一.界面显示 二.配置说明 1.名称:标识 2.注释:备注 3.Grouping (1.Number of si ...

  2. Jmeter之安装和配置

    一.Jmeter下载: 官网下载地址:http://jmeter.apache.org/download_jmeter.cgi 目前最新版本为5.0(未使用过),建议使用4.0 (存在两种格式的压缩包 ...

  3. python学习中

    python中的单引号.双引号.三引号的用法 网上也查找了资料,理解的都有些费劲 就自己验证了一下(主要是目前掌握的python知识,不知道什么时候会同时用到这三种引号) 用python3验证的 单引 ...

  4. 应用安全 - Web框架 - Apache Solr - 漏洞汇总

    CVE-2019-12409 Date: // 类型: 配置不当导致远程代码执行 前置条件: 影响范围: Solr and for Linux Solr下载:https://www.apache.or ...

  5. 使用itchat获取微信好友的男女比例

    # 使用itchat获取微信好友的男女比例 import itchat itchat.auto_login(hotReload=True) friends = itchat.get_friends(u ...

  6. HTTPS测试

    1.首先理解HTTPS的含义,清楚http与HTTPS的区别 2.相对于http而言,https更加安全,因 3.使用数字证书使传输更安全,数字证书使用keytool工具生成 测试准备: 创建公钥和秘 ...

  7. 刘铁猛-深入浅出WPF-系列资源汇总

    首先奉上原作者刘铁猛博客地址:http://www.cnblogs.com/prism/ 作者讲的很不错,没有之一,另外作者出了一本书,希望大家支持. 送上全套高清晰视频教程(我注册了3个51cto的 ...

  8. A+B and A*B problem 大数相加 相乘 模拟

    A+B and A*B problem 大数相加 相乘 模拟 题意 给你两个数a和b,这两个数很大,然后输出这两个数相加的和,相乘的积. 解题思路 模拟,但是还是搜了搜代码实现,发现这个大佬写的是真的 ...

  9. python3—廖雪峰之练习(二)

    函数的参数练习 请定义一个函数quadratic(a, b, c), 接收3个参数,返回一元二次方程 : $ ax^2+b+c=0 $ 的两个解 提示:计算平方根可以调用math.sqrt()函数: ...

  10. openmvg中cmd模块解析

    ---恢复内容开始--- 在openmvg库中,定义了一个CmdLine类来定义例程的输入参数格式.源文件为.\openMVG\src\third_party\cmdLine\cmdLine.h. 先 ...