final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable {} ]public Object invoke(Object proxy, Method method, Object[] arg... ...
final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable {} ]public Object invoke(Object proxy, Method method, Object[] args) >Object retVal; >List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass); >if (chain.isEmpty()) { >+Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args); >+retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse); >} >else { >+invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);//鏈式執行攔截(通知)方法 >+retVal = invocation.proceed(); >} [ReflectiveMethodInvocation.proceed()] 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; if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {
//調用攔截器的執行方法,攔截器執行攔截邏輯後繼續調用目標方法的proceed()方法,參考下麵的兩個攔截器invoke()實現 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); } } //before advice方法攔截器 public class MethodBeforeAdviceInterceptor implements MethodInterceptor, BeforeAdvice, Serializable { private final MethodBeforeAdvice advice; /** * Create a new MethodBeforeAdviceInterceptor for the given advice. * @param advice the MethodBeforeAdvice to wrap */ public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) { Assert.notNull(advice, "Advice must not be null"); this.advice = advice; } @Override public Object invoke(MethodInvocation mi) throws Throwable { this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis()); return mi.proceed(); } } //after advice方法攔截器 public class AspectJAfterAdvice extends AbstractAspectJAdvice implements MethodInterceptor, AfterAdvice, Serializable { public AspectJAfterAdvice( Method aspectJBeforeAdviceMethod, AspectJExpressionPointcut pointcut, AspectInstanceFactory aif) { super(aspectJBeforeAdviceMethod, pointcut, aif); } @Override public Object invoke(MethodInvocation mi) throws Throwable { try { return mi.proceed(); } finally { invokeAdviceMethod(getJoinPointMatch(), null, null); } } @Override public boolean isBeforeAdvice() { return false; } @Override public boolean isAfterAdvice() { return true; } }