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]
看看它的源码,什么都清楚了
[code="java"]
/**
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:
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
}
[/code]
便于以后升级,扩展吧,同下兼容.