// Invoked method threw a checked exception.
// We must rethrow it. The client won''t see the interceptor.
throw ex.getTargetException();
}
catch (IllegalArgumentException ex) {
throw new AopInvocationException("AOP configuration seems to be invalid: tried calling method [" +
method + "] on target [" + target + "]", ex);
}
catch (IllegalAccessException ex) {
throw new AopInvocationException("Couldn''t access method: " + method, ex);
}
}
对******链的调用处理是在ReflectiveMethodInvocation里实现的:
Java代码
public Object proceed() throws Throwable {
// We start with an index of -1 and increment early.
// 这里直接调用目标对象的方法,没有******的调用或者******已经调用完 了,这个currentInterceptorIndex的初始值是0
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size()) {
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.
// 这里获得相应的拦截器,如果拦截器可以匹配的上的话,那就调用拦 截器的invoke方法
InterceptorAndDynamicMethodMatcher dm =
(InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {
return dm.interceptor.invoke(nextInvocation());
}
else {
// Dynamic matching failed.
// Skip this interceptor and invoke the next in the chain.
// 如果拦截器匹配不上,那就调用下一个拦截器,这个时候拦截器 链的位置指示后移并迭代调用当前的proceed方法
this.currentInterceptorIndex++;
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(nextInvocation());
}
}
Spring源代码解析(七):Spring AOP中对拦截器调用的实现(3)
时间:2011-03-29 javaeye jiwenke
这里把当前的拦截器链以及在拦截器链的位置标志都clone到一个MethodInvocation对 象了,作用是当前的拦截器执行完之后,会继续沿着得到这个拦截器链执行下面的拦截行 为,也就是会迭代的调用上面这个proceed:
Java代码
private ReflectiveMethodInvocation nextInvocation() throws CloneNotSupportedException {
ReflectiveMethodInvocation invocation = (ReflectiveMethodInvocation) clone();
invocati
|