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
- How does Spring know that ThrowsAdvice.afterThrowing needs to be called?
- Windows Azure 本地存储 SetConfigurationSettingPublisher needs to be called before FromConfigurationSetting can be used
- What Every Driver Writer Needs to Know
- How to know that a triangle triple exists in one array?
- make 'FORCE' does not exist - don't know how to make it 解决
- How much math does a GIS Analyst need to know?(转)
- How does Gradle know whether to do a single or multiproject build?
- ORMLite does not know how to store class java.util.ArrayList错误的解决
- How to know firmware version
- log4j:WARN No appenders could be found for logger(Know how log4j tries to configure itself )
- Details of different phases of a spam-saint update and the logs that needs to be checked while impor
- Spring AOP之ThrowsAdvice
- Spring AOP之ThrowsAdvice
- 关于spring的ThrowsAdvice
- 关于spring的ThrowsAdvice
- How to be sure that message via socket.io has been received to the client?
- Foundations of Security: What Every Programmer Needs to Know
- 12 Things A Programmer Really Needs To Know
- 深入理解C++
- 规范命名
- 单例模式---设计模式(一)
- Git入门教程
- 黑马程序员_java学习笔记 5.java面向对象特性
- How does Spring know that ThrowsAdvice.afterThrowing needs to be called?
- java学习2-循环中的嵌套语句2
- Spring与Quartz的整合实现定时任务调度
- iPhone如何查询序列号 iPhone激活时间查询
- javascript深入理解js闭包
- mac10.10Apache配置笔记
- 锚点的使用(链接到网页中某个位置)
- PayPal Express Checkout开发
- 一个类里可以注册不同类型的消息及boost:function和boost:bind的使用