Interface MethodInterceptor
-
org.aopalliance.intercept.MethodInterceptor
- invoke(MethodInvocation invocation)
-
这是一个函数接口,因此可以用作lambda表达式或方法引用的赋值目标。
-
拦截在到达目标的过程中对接口的调用。它们嵌套在目标(target)的“顶部”。
-
用户应该实现invoke(MethodInvocation)方法来修改原始行为。例如:以下的类实现了一个追踪拦截器(追踪所有对拦截方法的调用)
invoke(MethodInvocation invocation)
- 实现此方法以在调用的方法之前和之后执行额外的处理。
- invocation:方法调用连接点Joinpoint
- 简洁的实现当然希望调用Joinpoint.proceed()。
- 调用Joinpoint.proceed()的结果;可能被拦截机拦截
使用场景
- 追踪拦截器
场景一:打印操作日志
,可扩展:‘添加调用链’
class TracingInterceptor implements MethodInterceptor {
Object invoke(MethodInvocation i) throws Throwable {
System.out.println("method "+i.getMethod()+" is called on "+
i.getThis()+" with args "+i.getArguments());
Object ret=i.proceed()