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.
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:
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:
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!
never gets executed, which means the @Validating doSomething()
method is never being intercepted the way the Guice docs show.
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?!?!
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!
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:
The method invoke(MethodInvocation) of type ValidatingMethodInterceptor must override a superclass method.
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.
See Question&Answers more detail:
os