有时需要知道截取Guice中的方法调用的方法拦截器的顺序。一个简单的示例场景是使用guice-persist提供的@Transactional方法拦截器和自定义的@Retry方法拦截器。重试拦截器必须在事务拦截器之外运行,以确保重试不会在同一事务内执行。
在Spring中,您可以使用拦截器的有序接口来确保事务拦截器在重试拦截器中执行。有没有办法在Guice中实现同样的目标?
发布于 2011-11-30 06:18:07
Guice按照拦截器注册的顺序调用拦截器。因此,如果你这样定义它们:
bindInterceptor(any(), annotatedWith(Retry.class), retryInterceptor);
bindInterceptor(any(), annotatedWith(Transactional.class), transactionalInterceptor);或
bindInterceptor(any(), annotatedWith(Retry.class), retryInterceptor, transactionalInterceptor);retryInterceptor将在transactionalInterceptor之前执行。
如果你有多个模块,同样适用--第一个模块的拦截器在第二个模块的拦截器之前执行,依此类推。
https://stackoverflow.com/questions/8308203
复制相似问题