我试图在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.