How does Spring know that ThrowsAdvice.afterThrowing needs to be called?

来源:互联网 发布:情景喜剧排行知乎 编辑:程序博客网 时间:2024/06/06 09:31

How does Spring know that ThrowsAdvice.afterThrowing needs to be called?

I found documentation on the class here but I was wondering if anyone has an elegant explanation of how exactly reflection is used to look for that “afterThrowing” method specificially. I just want to see the code that does that so I can understand it better.

You’re looking at very old documentation (though the current one doesn’t say much more).

Spring uses a ThrowsAdviceInterceptor to handle ThrowsAdvice. You can find version 4.1.4.RELEASE source code here.

Its constructor

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() + "]");    }}

scans for appropriate methods and registers them. It then wraps the target method invocation

@Overridepublic 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;    }}

and invokes the handler if an exception is thrown.

0 0
原创粉丝点击