有时需要知道 Guice 中拦截方法调用的方法拦截器的顺序。一个简单的示例场景是使用 guice-persist 提供的 @Transactional 方法拦截器和自定义 @Retry 方法拦截器。重试拦截器必须在事务拦截器外部运行,以确保重试不会在同一事务内执行。
在 Spring 中,您可以使用拦截器的 Ordered 接口来确保事务拦截器在重试拦截器内执行。有没有办法在 Guice 中达到同样的效果?
Guice 按照拦截器的注册顺序调用它们。所以如果你像这样定义它们:
bindInterceptor(any(), annotatedWith(Retry.class), retryInterceptor);
bindInterceptor(any(), annotatedWith(Transactional.class), transactionalInterceptor);
or
bindInterceptor(any(), annotatedWith(Retry.class), retryInterceptor, transactionalInterceptor);
the retryInterceptor
将在之前执行transactionalInterceptor
.
如果您有多个模块,则同样适用 - 第一个模块的拦截器在秒模块的拦截器之前执行,依此类推。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)