1、 around advice:所有around advice必須實現MethodInterceptor接口,注意invoke方法的參數invocation是MethodInvocation接口,在此中包含了許多信息,包括其所封裝的方法及其參數,AOP proxy、Jointcut等。
public interface MethodInterceptor extends Interceptor {
Object invoke(MethodInvocation invocation) throws Throwable;
}
在實現around advice時,和before advice、after advice有著兩個很大的區別:1、必須在invoke方法中調用MethodInvocation.proceed(),這樣才能將所有調用延續下去,調用target對象的method;2、必須自己返回一個object,該object甚至可以與target’s method的返回值不一樣。
2、 before advice:在jointcut執行之前,運行advice。必須實現MethodBeforeAdvice接口。
public interface MethodBeforeAdvice extends BeforeAdvice {
void before(Method m, Object[] args, Object target) throws Throwable;
}
3、 after advice:在jointcut執行之后,運行advice。必須實現AfterReturningAdvice接口。
public interface AfterReturningAdvice extends Advice {
void afterReturning(Object returnValue, Method m, Object[] args, Object target)
throws Throwable;
}
4、 throws advice:在jointcut執行出現異常的時候,運行此advice。必須實現ThrowsAdvice接口。但是此接口只是一個標識接口,必須實現此外實現下面的方法:
afterThrowing([Method], [args], [target], subclassOfThrowable)
此外,在jointcut出現異常時,具體調用哪個afterThrowing方法,這就涉及到類型判別,最符合類型判別的將會被調用。