spring4中文技术手册
在《静态代理和动态代理》中提到了面向方面编程,主要就是基于动态代理。单独抽象出非业务的功能,服务于某些业务方法。Spring提供了四种很实用的Advice,分别为:Before Advice, After Returning Advice, Around Advice, After throwing Advice。都是方法级别的,就是在某个方法执行前后插入一些非业务的操作,如打日志或者判断权限等。
对于这四种advice的实现,spring都提供了三种方法,分别为基于接口、基于xml和基于annotation(注释)。Before Advice会在目标对象的方法执行之前被调用;After Advice会在目标方法执行之后被调用;Around Advice则可以在目标方法执行前后同时加上相关服务;Throw Advice是在异常发生后执行某些操作。
这个就需要自定义的Aspect实现Spring接口。
.:
/**
* Advice invoked before a method is invoked. Such advices cannot
* prevent the method call proceeding, unless they throw a Throwable.
*/
public interface MethodBeforeAdvice extends BeforeAdvice {
/**
* Callback before a given method is invoked.
* ***@param method method being invoked
* ***@param args arguments to the method
* ***@param target target of the method invocation. May be <code>null</code>.
* ***@throws Throwable if this object wishes to abort the call.
* Any exception thrown will be returned to the caller if it's
* allowed by the method signature. Otherwise the exception
* will be wrapped as a runtime exception.
*/
void before(Method method, Object[] args, Object target) throws Throwable;
}
After .:
/**
* After returning advice is invoked only on norma
spring4中文技术手册== 来自淘豆网m.daumloan.com转载请标明出处.