This is indeed a bootstrapping issues of sorts. The common idiom to work around this is to call injector.injectMembers(interceptor).
-Fred On Fri, Apr 23, 2010 at 8:51 AM, AleBu <[email protected]> wrote: > Hi, > To apply interceptor I need to call binder's bindInterceptor method > with instance of interceptor provided. But what if I want use guice > for constructing interceptor too? How to specify guice the way to > construct interceptor and instruct guice to use that way? > > Example (situation): > I have a service for logging described as interface. There can be > different implementations for it (console, file, database, etc). In > different situations assembling such a service can be a complex task, > so I want guice to do it (Isn't it all about it?). I have an a logging > interceptor which want to utilize logging service. Logging interceptor > must somehow access to instance of logging service for actual logging. > And finally, I want to apply such logging interceptor for other > objects constructed by guice (well, it IS an interceptor after all). > > Problem as I see it: > I bind Logging service interface to implementation using binder's bind > method or define another way for guice to build loggin service (via > provider, etc). Then I am specifing guice how to construct logging > interceptor instance. The idea here is pass an logging service as a > parameter to interceptor constructor. At this point guice could > construct logging interceptor without any problems. It knows how to > instanciate it and it konws how to instanciate it parameter (logging > service). But then there is a problem, because to bind interceptor, I > need to construct it first, but I dont want it, instead, I want guice > to do it. As I understand, I can't request guice for objects in > module's 'configure' method. Maybe it's a wrong strategy at all, but > in this case - how it can be done in the right way? > > Code block displaying problem: > > Interceptor constructor: > > public LoggingInterceptor( @Inject LoggingService loggingService ){ /* > keep logging service reference for actual logging */ } > > Module construct method: > binder.bind( LoggingInterceptor.class).to ... > ... > binder.bindInterceptor( /* class matcher */, /* method matcher > */, ??? ); // somehow need to specify to use LoggingInterceptor > > ------ > > Perhaps, if bindInterceptor method could use > Provider<MethodInvocation> instead of concreate object, then it would > be possible to solve that problem. > > -- > You received this message because you are subscribed to the Google Groups > "google-guice" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<google-guice%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en. > > -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
