【发布时间】:2014-06-11 16:10:47
【问题描述】:
我们拥有 Guice 及其 AOP 支持。我们有两个使用 AOP 支持的 3d 派对模块:Shiro 和 Guice 的 JPA 模块。结果 Guice 抱怨“该方法可能被截获两次”。 我的问题是如何避免这种行为:我可能根本不需要拦截合成方法。
如果模块是我们的,我们可以添加一个 Matcher 过滤掉所有合成方法(就像它说的 here),但问题是这些是 3d 派对模块。
【问题讨论】:
我们拥有 Guice 及其 AOP 支持。我们有两个使用 AOP 支持的 3d 派对模块:Shiro 和 Guice 的 JPA 模块。结果 Guice 抱怨“该方法可能被截获两次”。 我的问题是如何避免这种行为:我可能根本不需要拦截合成方法。
如果模块是我们的,我们可以添加一个 Matcher 过滤掉所有合成方法(就像它说的 here),但问题是这些是 3d 派对模块。
【问题讨论】:
我能找到的最好方法如下:只需像这样覆盖 bindInterceptor 方法。
匹配器:
public final class NoSyntheticMethodMatcher extends AbstractMatcher<Method> {
public static final NoSyntheticMethodMatcher INSTANCE = new NoSyntheticMethodMatcher();
private NoSyntheticMethodMatcher() {}
@Override
public boolean matches(Method method) {
return !method.isSynthetic();
}
}
bindInterceptor 方法:
@Override
protected void bindInterceptor(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors) {
super.bindInterceptor(classMatcher, NoSyntheticMethodMatcher.INSTANCE.and(methodMatcher), interceptors);
}
但解决方案并不总是有效。就像我的情况一样,目标 JpaPersistModule 是最终的,我可以覆盖该方法的唯一方法是复制粘贴实现。
【讨论】: