How to define order of method interceptors in Guice?

穿精又带淫゛_ 提交于 2019-12-03 09:24:52

问题


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?


回答1:


Guice invokes the interceptors in the order in which they were registered. So if you define them something like this:

bindInterceptor(any(), annotatedWith(Retry.class), retryInterceptor);
bindInterceptor(any(), annotatedWith(Transactional.class), transactionalInterceptor);

or

bindInterceptor(any(), annotatedWith(Retry.class), retryInterceptor, transactionalInterceptor);

the retryInterceptor will be executed before the transactionalInterceptor.

Same applies if you have multiple modules - the interceptors from first module are executed before the interceptors of the seconds module and so on.



来源:https://stackoverflow.com/questions/8308203/how-to-define-order-of-method-interceptors-in-guice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!