无法让 Guice 方法拦截起作用 [英] Can't Get Guice Method Interception to Work

查看:27
本文介绍了无法让 Guice 方法拦截起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印你好,AOP!"每当 Guice/AOP 联盟拦截标有特定(自定义)注释的方法时,都会发送消息.我遵循了官方文档(可以找到的 PDF 此处 - 第 11 页上的 AOP 方法拦截内容)并且无法使其工作,只能编译.

I'm trying to print a "Hello, AOP!" message whenever Guice/AOP Alliance intercepts a method marked with a particular (custom) annotation. 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.

首先,我的注释:

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

然后,我的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!");
    }
}

最后,试图利用所有这些 AOP 东西的驱动程序:

Finally, the driver that attempts to make use of all this AOP stuff:

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();
    }
}

当我运行这个小测试驱动程序时,我得到的唯一控制台输出是 About to use AOP......Hello, AOP! 永远不会被执行,这意味着 @Validating doSomething() 方法永远不会像 Guice 文档显示的那样被拦截.

When I run this little test driver, the only console output I get is About to use AOP...... the Hello, AOP! never gets executed, which means the @Validating doSomething() method is never being intercepted the way the Guice docs show.

我能想到的唯一事情是,在我的 Module 实现中,我指定了要绑定到的 MethodInterceptor(作为bindInterceptor 方法的第三个参数)作为一个 new ValidatingMethodInterceptor(),而在那个拦截器中,我只定义了一个必需的 invoke(MethodInvocation) 方法.

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.

也许我没有正确地将这两个连接在一起?也许 Guice 并不隐含地知道发生拦截时应该运行 invoke 方法?!?!

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 文档,还遵循了其他几个教程,但都无济于事.

Then again, not only have I followed the Guice docs, I have also followed several other tutorials to no avail.

我在这里遗漏了什么明显的东西吗?提前致谢!

Is there something obvious I am missing here? Thanks in advance!

编辑我的代码和我遵循的示例之间的另一个差异虽然很小,但事实上我的 invoke 方法(在拦截器内部)没有用 <代码>@Override.如果我尝试添加此注释,则会出现以下编译错误:

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. If I try to add this annotation, I get the following compile error:

ValidatingMethodInterceptor 类型的方法 invoke(MethodInvocation) 必须覆盖超类方法.

The method invoke(MethodInvocation) of type ValidatingMethodInterceptor must override a superclass method.

这个错误是有道理的,因为 org.aopalliance.intercept.MethodInterceptor 是一个接口(不是一个类).再说一次,每个使用 Guice/AOP 联盟的例子都在 invoke 方法上使用这个 @Override 注释,所以它显然对某些人有效/编译......很奇怪.>

This error makes sense, because org.aopalliance.intercept.MethodInterceptor is an interface (not a class). 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 构造你的对象,它就不能为你提供一个包含拦截器的实例.您不得使用 new AopTest() 来获取对象的实例.相反,您必须要求 Guice 给您一个实例:

If you don't let Guice construct your object, it can't provide you an instance with the interceptor wrapped around. You must not use new AopTest() to get an instance of your object. Instead, you must ask Guice to give you one instance:

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

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

这篇关于无法让 Guice 方法拦截起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆