简体   繁体   English

无法进行Guice方法拦截

[英]Can't Get Guice Method Interception to Work

I'm trying to print a "Hello, AOP!" 我正在尝试打印“你好,AOP!” message whenever Guice/AOP Alliance intercepts a method marked with a particular (custom) annotation. 当Guice / AOP联盟拦截标有特定(自定义)注释的方法时,将显示一条消息。 I have followed the official docs (a PDF that can be found here - AOP method interception stuff on pg. 11 ) and cannot get it to work, only compile. 我遵循了官方文档(可在此处找到PDF-第11页上的AOP方法拦截内容 ),并且无法使其正常工作,只能进行编译。

First, my annotation: 首先,我的注释:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@BindingAnnotation
public @interface Validating {
    // Do nothing; used by Google Guice to intercept certain methods.
}

Then, my Module implementation: 然后,我的Module实现:

public class ValidatingModule implements com.google.inject.Module {
    public void configure(Binder binder) {
        binder.bindInterceptor(Matchers.any(), 
            Matchers.annotatedWith(Validating.class,
            new ValidatingMethodInterceptor()),
    }
}

Next, my method interceptor: 接下来,我的方法拦截器:

public class ValidatingMethodInterceptor implements MethodInterceptor {
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("Hello, AOP!");
    }
}

Finally, the driver that attempts to make use of all this AOP stuff: 最后,尝试利用所有这些AOP内容的驱动程序:

public class AopTest {
    @Validating
    public int doSomething() {
        // do whatever
    }

    public static main(String[] args) {
        AopTest test = new AopTest();

        Injector injector = Guice.createInjector(new ValidatingModule());

        System.out.println("About to use AOP...");

        test.doSomething();
    }
}

When I run this little test driver, the only console output I get is About to use AOP... ... the Hello, AOP! 当我运行这个小小的测试驱动程序时,我得到的唯一控制台输出是About to use AOP... ... Hello, AOP! never gets executed, which means the @Validating doSomething() method is never being intercepted the way the Guice docs show. 永远不会执行,这意味着@Validating doSomething()方法永远不会像Guice文档所示的那样被拦截。

The only thing I can think of is the fact that in my Module implementation I am specifying the MethodInterceptor to bind to (as the 3rd argument to the bindInterceptor method) as being a new ValidatingMethodInterceptor() , whereas in that interceptor, I am only defining a required invoke(MethodInvocation ) method. 唯一能想到的事实是,在我的Module实现中,我指定要绑定的MethodInterceptor (作为bindInterceptor方法的第三个参数)为new ValidatingMethodInterceptor() ,而在该拦截器中,我仅定义必需的invoke(MethodInvocation )方法。

Perhaps I am not wiring these two together correctly? 也许我没有正确地将这两者连接在一起? Perhaps Guice doesn't implicitly know that the invoke method should be ran when an intercept occurs?!?! 也许Guice并不隐式知道在发生拦截时应该运行invoke方法?!?!

Then again, not only have I followed the Guice docs, I have also followed several other tutorials to no avail. 再说一次,我不仅遵循了Guice文档,而且还遵循了其他一些教程,但无济于事。

Is there something obvious I am missing here? 有什么明显的我想念的地方吗? Thanks in advance! 提前致谢!

Edit One other discrepancy between my code and the examples I followed, although small, is the fact that my invoke method (inside the interceptor) is not annotated with @Override . 编辑我的代码与所遵循的示例之间的另一个差异(尽管很小)是这样的事实,即我的invoke方法(在拦截器内部)未使用@Override注释。 If I try to add this annotation, I get the following compile error: 如果我尝试添加此批注,则会出现以下编译错误:

The method invoke(MethodInvocation) of type ValidatingMethodInterceptor must override a superclass method. ValidatingMethodInterceptor类型的方法invoke(MethodInvocation)必须重写超类方法。

This error makes sense, because org.aopalliance.intercept.MethodInterceptor is an interface (not a class). 该错误是有道理的,因为org.aopalliance.intercept.MethodInterceptor是一个接口(不是类)。 Then again, every example using Guice/AOP Alliance uses this @Override annotation on the invoke method, so it obviously works/compiles for some people...weird. 再说一次,每个使用Guice / AOP Alliance的示例在invoke方法上都使用此@Override注释,因此它显然可以对某些人起作用/编译...很奇怪。

If you don't let Guice construct your object, it can't provide you an instance with the interceptor wrapped around. 如果您不让Guice构造您的对象,那么它就无法为您提供一个包裹着拦截器的实例。 You must not use new AopTest() to get an instance of your object. 您不得使用new AopTest()获取对象的实例。 Instead, you must ask Guice to give you one instance: 相反,您必须要求Guice给您一个实例:

Injector injector = Guice.createInjector(new ValidatingModule ());
AopTest test = injector.getInstance(AopTest.class);

See http://code.google.com/p/google-guice/wiki/GettingStarted 请参阅http://code.google.com/p/google-guice/wiki/GettingStarted

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM