关于spring的ThrowsAdvice

 

   aop的一个切面接口是 ThrowsAdvice,这是个标记接口,里面没有定义任何方法。书上说,根据spring文档,必须定义一个 afterThrowing([Method, args, target], subclassOfThrowable) 形式的方法,前面三个参数可选,也就是你可以写成 afterThrowing( args, target, subclassOfThrowable) ,也可以写成 afterThrowing( target, subclassOfThrowable)

事实上如果真的这么做,运行时会抛出 At least one handler method must be found in class 形式的异常。在确认自己没有打错字之后,只好去查spring2.0的手册,才发现上面是这么说的:方法可以有一个或四个参数。 也就是说,不能有两个、三个参数,方法的形式只能有两种: afterThrowing([Method, args, target], subclassOfThrowable) 或者 afterThrowing( subclassOfThrowable)

看看它的源码,什么都清楚了

Java代码
  1. /**
  2. * Tag interface for throws advice.
  3. *
  4. * <p>There are not any methods on this interface, as methods are invoked by reflection.
  5. //反正spring它内部自己知道用反射来匹配调用这个方法名的方法就行了,
  6. Implementing classes must implement methods of the form:<br>
  7. *
  8. * <code>void afterThrowing([Method, args, target], ThrowableSubclass);</code>
  9. *
  10. * <p>Some examples of valid methods would be:
  11. * 这里有4个方法,也就是支持4种方式,如果都定义在接口里,用户实现接口那必然
  12. * 要把4个方法都实现,(但却只需要用一个方法时)很冗余吧,所以就干脆定义成标识接口
  13. * <pre class="code">public void afterThrowing(Exception ex)</pre>
  14. * <pre class="code">public void afterThrowing(RemoteException)</pre>
  15. * <pre class="code">public void afterThrowing(Method method, Object[] args, Object target, Exception ex)</pre>
  16. * <pre class="code">public void afterThrowing(Method method, Object[] args, Object target, ServletException ex)</pre>
  17. *
  18. * <p>The first three arguments are optional, and only useful if
  19. * we want further information about the joinpoint, as in AspectJ
  20. * <b>after throwing</b> advice.
  21. *
  22. * @author Rod Johnson
  23. * @see AfterReturningAdvice
  24. * @see MethodBeforeAdvice
  25. */  
  26. public interface ThrowsAdvice extends AfterAdvice {   
  27.   
  28. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值