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)
看看它的源码,什么都清楚了
- /**
- * Tag interface for throws advice.
- *
- * <p>There are not any methods on this interface, as methods are invoked by reflection.
- //反正spring它内部自己知道用反射来匹配调用这个方法名的方法就行了,
- Implementing classes must implement methods of the form:<br>
- *
- * <code>void afterThrowing([Method, args, target], ThrowableSubclass);</code>
- *
- * <p>Some examples of valid methods would be:
- * 这里有4个方法,也就是支持4种方式,如果都定义在接口里,用户实现接口那必然
- * 要把4个方法都实现,(但却只需要用一个方法时)很冗余吧,所以就干脆定义成标识接口
- * <pre class="code">public void afterThrowing(Exception ex)</pre>
- * <pre class="code">public void afterThrowing(RemoteException)</pre>
- * <pre class="code">public void afterThrowing(Method method, Object[] args, Object target, Exception ex)</pre>
- * <pre class="code">public void afterThrowing(Method method, Object[] args, Object target, ServletException ex)</pre>
- *
- * <p>The first three arguments are optional, and only useful if
- * we want further information about the joinpoint, as in AspectJ
- * <b>after throwing</b> advice.
- *
- * @author Rod Johnson
- * @see AfterReturningAdvice
- * @see MethodBeforeAdvice
- */
- public interface ThrowsAdvice extends AfterAdvice {
- }