调用了CglibAopProxy.DynamicAdvisedInterceptor.intercept()
方法
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
这个方法得到一个拦截器链, 是5个增强器封装成MethodInterceptor
然后 mi.proceed() 方法触发拦截器链的方法。
@Override
@Nullable
public Object proceed() throws Throwable {
// We start with an index of -1 and increment early.
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
return invokeJoinpoint();
}
Object interceptorOrInterceptionAdvice =
this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
// Evaluate dynamic method matcher here: static part will already have
// been evaluated and found to match.
InterceptorAndDynamicMethodMatcher dm =
(InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
Class<?> targetClass = (this.targetClass != null ? this.targetClass : this.method.getDeclaringClass());
if (dm.methodMatcher.matches(this.method, targetClass, this.arguments)) {
return dm.interceptor.invoke(this);
}
else {
// Dynamic matching failed.
// Skip this interceptor and invoke the next in the chain.
return proceed();
}
}
else {
// It's an interceptor, so we just invoke it: The pointcut will have
// been evaluated statically before this object was constructed.
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
}
}
最后的一个方法进入了 invoke(this)
, 进入了拦截器的责任链
ExposeInvocationInterceptor, MethodBeforeAdviceInterceptor, AspectJAfterAdvice, AfterReturningAdviceInterceptor, AspectJAfterThrowingAdvice为Filter类
CglibMethodInvocation 为 FilterChain
这个拦截器为暴露共享数据
ExposeInvocationInterceptor.invoke()
先是 invocation.set(mi);
这里的invocation
是一个threadLocal
, 可以保存共享数据
又进入了ReflectiveMethodInvocation.proceed()
前置通知拦截器
MethodBeforeAdviceInterceptor.invoke()
先是执行了前置通知, 然后返回mi.procced()
后置通知拦截器
AspectJAfterAdvice.invoke()
返回的是 mi.proceed()
, 并进行 invokeAdviceMethod()
返回通知拦截器
AfterReturningAdviceInterceptor.invoke()
异常通知
AspectJAfterThrowingAdvice.invoke()
执行连接点, 用于执行方法
前置通知 => 目标方法 => 返回通知 => 后置通知
前置通知 => 目标方法 => 异常通知 => 后置通知
try{
前置通知
目标方法
返回通知
}catch(){
异常通知
}finally{
后置通知
}