bubble1000 2009-07-16 22:18
浏览 498
已采纳

Spring 中 Around AOP 实现了 ThrowsAdvice 接口后,为什么必须写一个方法名是 afterThrowing 的方法?

Spring 中 Around AOP 实现了 ThrowsAdvice 接口后,为什么必须写一个方法名是 afterThrowing 的方法?
不然会报错:
At least one handler method must be found in class
,既然如此,这个方法为什么不在接口里定义呢?
熟悉Spring的人讲讲。

[code="java"]
package hello;

import org.springframework.aop.ThrowsAdvice;

public class LoggingThrowsAdvice implements ThrowsAdvice{

public  void  afterThrowing(Exception  ex)  throws  Throwable{
    LogUtil.log.debug("shen  he  shu  ju  you  yi  chang...");
} 

}

[/code]

  • 写回答

2条回答 默认 最新

  • wanghaolovezlq 2009-07-16 23:13
    关注

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

    [code="java"]
    /**

    • Tag interface for throws advice. *
    • There are not any methods on this interface, as methods are invoked by reflection. //反正spring它内部自己知道用反射来匹配调用这个方法名的方法就行了, Implementing classes must implement methods of the form:

    • void afterThrowing([Method, args, target], ThrowableSubclass);
    • Some examples of valid methods would be:

    • 这里有4个方法,也就是支持4种方式,如果都定义在接口里,用户实现接口那必然
    • 要把4个方法都实现,(但却只需要用一个方法时)很冗余吧,所以就干脆定义成标识接口
    • public void afterThrowing(Exception ex)
    • public void afterThrowing(RemoteException)
    • public void afterThrowing(Method method, Object[] args, Object target, Exception ex)
    • public void afterThrowing(Method method, Object[] args, Object target, ServletException ex)
      *
    • The first three arguments are optional, and only useful if

    • we want further information about the joinpoint, as in AspectJ
    • after throwing advice. *
    • @author Rod Johnson
    • @see AfterReturningAdvice
    • @see MethodBeforeAdvice */ public interface ThrowsAdvice extends AfterAdvice {

    }

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?