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