关于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. }  

你可能感兴趣的:(关于spring的ThrowsAdvice)