Modules are the rules that configure an Injector. While reading the rules, it's not possible to also apply them. Applying them happens after they're all read, which is your second test.
sam On Mon, Feb 24, 2014 at 12:55 PM, Olivier Grégoire <[email protected]>wrote: > Hello! > > I have the SSCCE code written below. Why can't I use requestInjection in a > module and have my object injected at the time of request? How do I > *actually* inject an object when I *know* all its dependencies have been > provided? > > The problem I try to solve is that I need different implementations based > on properties. In a previous module, I bound those properties using > Names.bindProperties(). I will not create all the dependencies then chose > which one to use afterwards. Do I really need to pass my Properties around > the various modules? > > Thanks. > > Olivier > > > public class RequestInjectionTest { > @Test public void testRequestInjectionInModule() { // fails > Guice.createInjector(new AbstractModule() { > @Override protected void configure() { > final Holder holder = new Holder(); > > bind(String.class).annotatedWith(Names.named("value")).toInstance("value"); > requestInjection(holder); > Assert.assertEquals("value", holder.value); > } > }); > } > > @Test public void testRequestInjectionOutsideOfModule() { //Succeeds > final Holder holder = new Holder(); > Guice.createInjector(new AbstractModule() { > @Override protected void configure() { > > bind(String.class).annotatedWith(Names.named("value")).toInstance("value"); > requestInjection(holder); > } > }); > Assert.assertEquals("value", holder.value); > } > > public static class Holder { > @Inject @Named("value") public String value; > } > } > > > -- > Olivier Grégoire > > -- > You received this message because you are subscribed to the Google Groups > "google-guice" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/google-guice. > For more options, visit https://groups.google.com/groups/opt_out. > -- You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/google-guice. For more options, visit https://groups.google.com/groups/opt_out.
