正常情况下,每个Advisor里面都会包含PointCut和一个
Advice:pointCut用来判断方法和类是否需要被增强,Advice用来给Bean做增强。
调用步骤:
1、首先CGLib内部会根据方法获取对应的增强器
2、调用增强器的增强方法,对于MethodInterceptor的增强器,就会调用intercept方法。
2.1、intercept方法分析。我们分析对于自己写的@Aspect(@Around、@Before、@After 等)对于此拦截器,首先会对这个类下面的增强器与方法进行匹配,选择处理匹配的增强器。
2.2 如果数量不为0,则构造
CglibMethodInvocation。并进行调用
CglibMethodInvocation的
proceed()方法,里面会按照
代码定义的顺序倒叙执行,例如:先执行@Return中的process()方法,之后再里面里面进行递归的调用。
在前面分析过了AOP是如何发现需要增强的bean,以及如何通过动态代理的方式将进行增强的。
下面我们将来分析对于
CglibAopProxy增强过的Bean是如何进行调用的。在拦截器中做了什么操作。
通过上一篇文章的末尾,我们发现对于增强过的Bean,对于增强的方法默认的拦截器为DynamicAdvisedInterceptor。
下面我们将先来分析一下这个DynamicAdvisedInterceptor这个类。
private static class DynamicAdvisedInterceptor implements MethodInterceptor, Serializable {
private final AdvisedSupport advised;
public DynamicAdvisedInterceptor(AdvisedSupport advised) {
this.advised = advised;
}
@Override
@Nullable
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
Object oldProxy = null;
boolean setProxyContext = false;
Object target = null;
//实例的来源,对于单例模式,会返回同一个实例。对于原型模式会返回不同的值。
//具体详见: (转载)Spring 为什么需要TargetSource?
TargetSource targetSource = this.advised.getTargetSource();
try {
if (this.advised.exposeProxy) {
// Make invocation available if necessary.
oldProxy = AopContext.setCurrentProxy(proxy);
setProxyContext = true;
}
// Get as late as possible to minimize the time we "own" the target, in case it comes from a pool...
//实例的来源可以来自一个实例的池子,方便进行管理
target = targetSource.getTarget();
Class<?> targetClass = (target != null ? target.getClass() : null);
//从所有的增强器中获取此方法可以用的增强器 具体详见下面
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
Object retVal;
// Check whether we only