Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.15 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Can't Get Guice Method Interception to Work
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target({ElementType.METHOD})
  4. @BindingAnnotation
  5. public @interface Validating {
  6.     // Do nothing; used by Google Guice to intercept certain methods.
  7. }
  8.        
  9. public class ValidatingModule implements com.google.inject.Module {
  10.     public void configure(Binder binder) {
  11.         binder.bindInterceptor(Matchers.any(),
  12.             Matchers.annotatedWith(Validating.class,
  13.             new ValidatingMethodInterceptor()),
  14.     }
  15. }
  16.        
  17. public class ValidatingMethodInterceptor implements MethodInterceptor {
  18.     public Object invoke(MethodInvocation invocation) throws Throwable {
  19.         System.out.println("Hello, AOP!");
  20.     }
  21. }
  22.        
  23. public class AopTest {
  24.     @Validating
  25.     public int doSomething() {
  26.         // do whatever
  27.     }
  28.  
  29.     public static main(String[] args) {
  30.         AopTest test = new AopTest();
  31.  
  32.         Injector injector = Guice.createInjector(new ValidatingModule());
  33.  
  34.         System.out.println("About to use AOP...");
  35.  
  36.         test.doSomething();
  37.     }
  38. }
  39.        
  40. Injector injector = Guice.createInjector(new ValidatingModule ());
  41. AopTest test = injector.getInstance(AopTest.class);