English 中文(简体)
Can t Getice Approach Interception to Work
原标题:Can t Get Guice Method Interception to Work

我试图在Guice/OP联盟每次打上“Hello, AOP”电文时,就拦截了一种带有特定(tom)说明的方法。 我遵循了官方数字(见 - AOP 拦截在pg. 11上的障碍,不能让它去工作,只能汇编。

第一,我的说明:

@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()),
    }
}

其次,我的方法拦截器:

public class ValidatingMethodInterceptor implements MethodInterceptor {
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("Hello, 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();
    }
}

当我驾驶这一微薄的测试驱动力时,我获得的唯一光辉产出是<代码>。 供使用 <代码>Hello,AOP!从未得到执行,这意味着@ 定购单()方法从未被拦截。

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.

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 docs,我也跟着其他几门辅导,没有结果。

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

认证方法 方法使用者必须超越超级方法。

这一错误是有道理的,因为org.aopalliance.intercept.MethodInterceptor 是一个接口(不是一类)。 接着,使用Guice/AOP联盟的每一个例子都使用这个@。 Override annotation on the invoke methods, so this it clear work/compiles for some people...weird.

最佳回答
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签