Guice 执行后方法拦截 [英] Guice post execution method interception

查看:26
本文介绍了Guice 执行后方法拦截的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Guice 中,有没有办法让我的 MethodInterceptor::invoke 实现在被拦截的方法执行之后(而不是之前)被调用?

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

我已将当前代码添加到我的 AbstractModule:

I've added the current code to my AbstractModule:

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

推荐答案

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

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
   }
}

这篇关于Guice 执行后方法拦截的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆