如何在Guice中定义方法拦截器的顺序?

tau*_*nen 11 methods aop guice interceptor

有时候需要知道拦截Guice中方法调用的方法拦截器的顺序.一个简单的示例场景是使用guice-persist提供的@Transactional方法拦截器和自定义的@Retry方法拦截器.重试拦截器必须在事务拦截器之外运行,以确保重试不在同一事务中执行.

在Spring中,您可以使用拦截器的Ordered接口来确保在重试拦截器中执行事务拦截器.有没有办法在Guice中实现同样的目标?

Gen*_*sky 18

Guice按照它们的注册顺序调用拦截器.所以如果你定义它们是这样的:

bindInterceptor(any(), annotatedWith(Retry.class), retryInterceptor);
bindInterceptor(any(), annotatedWith(Transactional.class), transactionalInterceptor);
Run Code Online (Sandbox Code Playgroud)

要么

bindInterceptor(any(), annotatedWith(Retry.class), retryInterceptor, transactionalInterceptor);
Run Code Online (Sandbox Code Playgroud)

retryInterceptor会前被执行transactionalInterceptor.

如果您有多个模块,则同样适用 - 来自第一个模块的拦截器在秒模块的拦截器之前执行,依此类推.