Generally speaking, it's a circular problem to have bindings in an Injector depend on inejcted elements from that same Injector. This is because you of course need to create an injector before injecting anything, and adding bindings is part of the creation logic.
That said, I'd like to clarify what you mean by "injected" below: > What I really wanted to ask about is more specific problem. > Is it possible for Module to make totally different sets bindings, > depending on what is specified > in injected Configuration. Do you mean "injected" in the Guice-specific meaning, or as a general term which would apply also, e.g., to passing a argument into the Module's constructor? In the latter case you can certainly add bindings "dynamically." On Fri, Jun 17, 2011 at 4:16 PM, Łukasz Osipiuk <[email protected]> wrote: > > > On Jun 17, 7:13 pm, Fred Faber <[email protected]> wrote: > > You can affect your provision logic based on bindings, sure. > > > > public class SomeOtherModule { > > @Override protected void configure() { > > bind(ThingThatDependsOnAppProperties.class) > > .toProvider(new Provider<ThingThatDependsOnAppProperties>() { > > @Inject AppProperties appProperties; > > > > @Override public ThingThatDependsOnAppProperties get() > { > > if (appProperties.isSomethingDefined()) { > > return ...; > > } else { > > return ..; > > } > > }}); > > } > > > > } > > > > You can do the same with a provider method. > > Thanks. > > Actually I use this approach but it comes with limitations. > Module always exposes same set of bound interfaces - only > implementations may differ based on > configuration parameters read. > > What I really wanted to ask about is more specific problem. > Is it possible for Module to make totally different sets bindings, > depending on what is specified > in injected Configuration. > > Making an example: lets asume Configuration specifies set of values: > > X.a = 1 > X.b = 2 > X.c = 3 > > Can I have logic in Module which iterates over given injected > configuration and for each key starting with "X." it binds > X.class annotaded with @Named(suffix(key)) to some instance > XImpl(configuration.getValue(key)))? > > It is very specific example but I hope it illustrates general class of > problems I cannot manage with guice. > > > > But this comes with the caveat that conditional bindings do make for > tougher > > debugging, and certainly less insightful static analysis. > > Yes - I agree that it is usually true but there are situations where > conditional logic in modules > can lead to shorter and more manageable code. > > Regards, Łukasz > > > > > > > > > On Fri, Jun 17, 2011 at 10:15 AM, Łukasz Osipiuk <[email protected]> > wrote: > > > Hi! > > > > > I have a quick question concerning guice. I guess answer to it is "no" > > > but maybe I am wrong, and if so my life would be easier - so here it > > > goes. > > > > > Is it possible for Module.configure() method to somehow access > > > dependencies defined by other modules passed to > > > Guice.createInjector()? > > > > > Lets assume I have PropertiesModule which exposes implementation of > > > @AppProperties Configuration. > > > > > Is it possible for SomeOtherModule to get instance of @AppProperties > > > Configuration as it was bound by PropertiesModule and perform > > > differently depending on what is set in configuration? > > > > > If this is possible I would really appreciate a code snippet what is > > > correct way of doing this. > > > > > Regards, Łukasz Osipiuk > > > > > -- > > > 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. > > -- > 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. > > -- 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.
