无法获取Guice方法拦截工作 [英] Can't Get Guice Method Interception to Work

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

问题描述

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

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.
}

然后,我的模块实现:

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

当我运行这个小测试驱动程序时,唯一的控制台我得到的输出是关于使用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.

只有我能想到的事实是在我的模块实现中我指定 MethodInterceptor 绑定到(作为 bindInterceptor 方法的第3个参数)为新的ValidatingMethodInterceptor(),而在那拦截器,我只定义了一个必需的调用(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没有隐含地知道拦截发生时应该运行调用方法?!?!

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类型的方法调用(MethodInvocation)必须覆盖超类方法。

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

此错误有意义,因为 org.aopalliance.intercept.MethodInterceptor 是一个接口(不是一个类)。然后,每个使用Guice / AOP Alliance的示例都在调用方法上使用此 @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天全站免登陆