我正在使用 Guice 的方法拦截功能。我需要知道的是如何正确实现多个拦截器,形式如下:
this.bindInterceptor(Matchers.any(), Matchers.any(), new Interceptor1(), new Interceptor2());
具体来说,如果在两个拦截器中都调用了proceed(),会发生什么?被截获的方法会被调用两次吗?还是第一个拦截器中的继续()调用第二个拦截器,然后调用该方法?还是应该只有一个拦截器有一个proceed()?
谢谢
我正在使用 Guice 的方法拦截功能。我需要知道的是如何正确实现多个拦截器,形式如下:
this.bindInterceptor(Matchers.any(), Matchers.any(), new Interceptor1(), new Interceptor2());
具体来说,如果在两个拦截器中都调用了proceed(),会发生什么?被截获的方法会被调用两次吗?还是第一个拦截器中的继续()调用第二个拦截器,然后调用该方法?还是应该只有一个拦截器有一个proceed()?
谢谢
两个拦截器都可以(并且应该)调用proceed. 通过这种方式,它们可以用作独立的方面(即事务和日志记录)。实际上,如果您不从外部拦截器调用继续,那么下一个拦截器将不会触发。
方法拦截器将根据调用顺序以类似堆栈的方式bindInterceptor调用。在您的示例中,它看起来像这样:
Interceptor1 entry
Interceptor1 proceed
Interceptor2 entry
Interceptor2 proceed
Method
Interceptor2 exit
Interceptor1 exit