【问题标题】:Multiple method interceptions in GuiceGuice 中的多个方法拦截
【发布时间】:2014-07-23 03:48:57
【问题描述】:

我正在使用 Guice 的方法拦截功能。我需要知道的是如何正确实现多个拦截器,形式如下:

this.bindInterceptor(Matchers.any(), Matchers.any(), new Interceptor1(), new Interceptor2());

具体来说,如果在两个拦截器中都调用了proceed(),会发生什么?被截获的方法会被调用两次吗?还是第一个拦截器中的继续()调用第二个拦截器,然后调用该方法?还是应该只有一个拦截器有proceed()?

谢谢

【问题讨论】:

标签: java guice method-interception


【解决方案1】:

两个拦截器都可以(并且应该)调用proceed。这样它们就可以用作独立的方面(即事务和日志记录)。实际上,如果您不从外部拦截器调用继续,那么下一个拦截器将不会触发。

方法拦截器将根据bindInterceptor 调用的顺序以类似堆栈的方式调用。在您的示例中,它看起来像这样:

Interceptor1 entry
Interceptor1 proceed
  Interceptor2 entry
  Interceptor2 proceed
    Method
  Interceptor2 exit
Interceptor1 exit

【讨论】:

  • 这就是我的预感,但我想确定一下。谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-10-10
  • 1970-01-01
  • 1970-01-01
  • 2012-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多