Guice合成方法警告

Ale*_*nas 5 java aop jpa guice shiro

我们正在使用Guice及其AOP支持.我们有两个使用AOP支持的3d派对模块:Shiro和Guice的JPA模块.结果Guice抱怨"该方法可能被截获两次".我的问题是如何避免这种行为:我可能根本不需要拦截合成方法.

如果模块是我们的模块,我们可以添加一个Matcher来过滤掉所有合成方法(就像它在这里说的那样),但问题是这些是3d方模块.

Ale*_*nas 9

我能找到的最好方法如下:只需覆盖这样的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();
    }
}
Run Code Online (Sandbox Code Playgroud)

bindInterceptor方法:

@Override
protected void bindInterceptor(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors) {
    super.bindInterceptor(classMatcher, NoSyntheticMethodMatcher.INSTANCE.and(methodMatcher), interceptors);
}
Run Code Online (Sandbox Code Playgroud)

但解决方案并不总是有效.就像在我的情况下,目标JpaPersistModule是最终的,我可以覆盖该方法的唯一方法是复制粘贴实现.