Guice后置执行方法拦截

3
在Guice中,我是否有一种方法可以让我的MethodInterceptor :: invoke 实现在拦截的方法执行后(而不是立即之前)被调用?
我已将当前代码添加到我的AbstractModule中:
bindInterceptor(Matchers.subclassesOf(InterceptedClass.class), Matchers.annotatedWith(MyMethodAnnotation.class), new MyMethodInterceptor());
1个回答

7

在拦截器中执行方法调用后的代码(这不仅适用于Guice),您需要使用try/finally组合:

public Object invoke(MethodInvocation invocation) throws Throwable {
   try {
      // code run before execution

      return invocation.proceed();
   } finally {
      // code run after execution
   }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接