Injection dependency in MethodInterceptor

I have a MethodInterceptor with dependencies. How could I introduce them?

Here in 2007, Bob Lee said that this feature should be included in the next release, but I can not find an API for this. bindInterceptor method requires instances instead of classes.

+4
source share
1 answer

From the Guice Frequently Asked Questions :

To inject dependencies into the AOP MethodInterceptor, use requestInjection() next to the standard bindInterceptor () call.

 public class NotOnWeekendsModule extends AbstractModule { protected void configure() { MethodInterceptor interceptor = new WeekendBlocker(); requestInjection(interceptor); bindInterceptor(any(), annotatedWith(NotOnWeekends.class), interceptor); } } 

Another option is to use Binder.getProvider and pass the dependency in the interceptor constructor.

 public class NotOnWeekendsModule extends AbstractModule { protected void configure() { bindInterceptor(any(), annotatedWith(NotOnWeekends.class), new WeekendBlocker(getProvider(Calendar.class))); } } 
+10
source

All Articles