Guice post execution method interception

拥有回忆 提交于 2019-12-13 15:46:50

问题


In Guice, is there a way for my MethodInterceptor::invoke implementation to be invoked after the intercepted method is executed (and not immediately before)?

I've added the current code to my AbstractModule:

bindInterceptor(Matchers.subclassesOf(InterceptedClass.class), Matchers.annotatedWith(MyMethodAnnotation.class), new MyMethodInterceptor());

回答1:


To execute code after the method invocation in an interceptor (this applies not just to Guice), you have to use a try/finally combination:

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

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


来源:https://stackoverflow.com/questions/33195223/guice-post-execution-method-interception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!