如何在 Guice 中定义方法拦截器的顺序?
有时需要知道 Guice 中拦截方法调用的方法拦截器的顺序。一个简单的示例场景是使用 guice-persist 提供的 @Transactional 方法拦截器和自定义 @Retry 方法拦截器。重试拦截器必须在事务拦截器外部运行,以确保重试不会在同一事务内执行。
在 Spring 中,您可以使用拦截器的 Ordered 接口来确保事务拦截器在重试拦截器内执行。有没有办法在 Guice 中达到同样的效果?
Sometimes there's a need to know the order of method interceptors that intercept a method call in Guice. A simple example scenario would be to use guice-persist provided @Transactional method interceptor with a custom @Retry method interceptor. The retry interceptor must be run outside of the transactional interceptor to make sure the retries are not executed within the same transaction.
In Spring you could use the Ordered interface for the interceptor to make sure the transaction interceptor is executed within the retry interceptor. Is there a way to achieve the same in Guice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Guice 按照拦截器的注册顺序调用它们。因此,如果您像这样定义它们:
或者
retryInterceptor
将在transactionalInterceptor
之前执行。如果您有多个模块,同样适用 - 第一个模块的拦截器在秒模块的拦截器之前执行,依此类推。
Guice invokes the interceptors in the order in which they were registered. So if you define them something like this:
or
the
retryInterceptor
will be executed before thetransactionalInterceptor
.Same applies if you have multiple modules - the interceptors from first module are executed before the interceptors of the seconds module and so on.