2015-04-02 99 views

回答

2

您正在查看非常旧的文档(尽管current one没有多说)。

Spring使用ThrowsAdviceInterceptor来处理ThrowsAdvice。你可以找到版本4.1.4.RELEASE源代码here

它的构造

public ThrowsAdviceInterceptor(Object throwsAdvice) { 
    Assert.notNull(throwsAdvice, "Advice must not be null"); 
    this.throwsAdvice = throwsAdvice; 

    Method[] methods = throwsAdvice.getClass().getMethods(); 
    for (Method method : methods) { 
     if (method.getName().equals(AFTER_THROWING) && 
       (method.getParameterTypes().length == 1 || method.getParameterTypes().length == 4) && 
       Throwable.class.isAssignableFrom(method.getParameterTypes()[method.getParameterTypes().length - 1]) 
      ) { 
      // Have an exception handler 
      this.exceptionHandlerMap.put(method.getParameterTypes()[method.getParameterTypes().length - 1], method); 
      if (logger.isDebugEnabled()) { 
       logger.debug("Found exception handler method: " + method); 
      } 
     } 
    } 

    if (this.exceptionHandlerMap.isEmpty()) { 
     throw new IllegalArgumentException(
       "At least one handler method must be found in class [" + throwsAdvice.getClass() + "]"); 
    } 
} 

扫描的适当的方法和它们注册。然后它包装目标方法调用

@Override 
public Object invoke(MethodInvocation mi) throws Throwable { 
    try { 
     return mi.proceed(); 
    } 
    catch (Throwable ex) { 
     Method handlerMethod = getExceptionHandler(ex); 
     if (handlerMethod != null) { 
      invokeHandlerMethod(mi, ex, handlerMethod); 
     } 
     throw ex; 
    } 
} 

并在引发异常时调用处理函数。

+0

谢谢,那正是我一直在寻找的。很高兴看到AFTER_THROWING静态成员。 – djangofan 2015-04-03 02:00:08