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

标签 methods aop guice interceptor

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

在 Spring 中,您可以使用拦截器的 Ordered 接口(interface)来确保事务拦截器在重试拦截器中执行。有没有办法在 Guice 中实现同样的效果?

最佳答案

Guice 按照它们注册的顺序调用拦截器。因此,如果您将它们定义如下:

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

或者
bindInterceptor(any(), annotatedWith(Retry.class), retryInterceptor, transactionalInterceptor);
retryInterceptor将在 transactionalInterceptor 之前执行.

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

关于methods - 如何在 Guice 中定义方法拦截器的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8308203/

相关文章:

java - 如何在 Swing 应用程序中使用 Guice

javascript - 如何使用javascript检查string1中是否包含一个或多个字符串

javascript - 在javascript中过滤数组内对象内的数组

java - 如何将 AspectJ 加载时编织与 Spring AOP 结合使用?

spring - 为什么spring不依赖aspectj会抛出aspectj错误?

java - Guice log4j 自定义注入(inject)不支持在构造函数内进行日志记录

swift - "0"无法快速转换为BackgroundView

java - 如何在编辑输出时读取文件并创建新文件?

java - 如何使用 aspectJ 快速修改已编译的 java 类行为

java - Guice + Tomcat 潜在的内存泄漏