Hi Folks,
Since upgrading to the latest snapshot, I'm trying to convert some code to
use the newer features. In attempting to use createChildInjector, it looks
like the child injector doesn't eagerly create all singleton. I've included
a test case for this at the end of the email.
We used to have a fake child-injector doing something like:
Injector child = Guice.createInjector(Stage.PRODUCTION,
providersFrom(parent), new OtherModules());
where providersFrom was:
public static Module providersFrom(final Injector parent) {
return new Module() {
public void configure(Binder binder) {
Key loggerKey = Key.get(Logger.class);
Key injectorKey = Key.get(Injector.class);
Key stageKey = Key.get(Stage.class);
for(Map.Entry<Key<?>, Binding<?>> entry :
parent.getBindings().entrySet()) {
Key key = entry.getKey();
Binding binding = entry.getValue();
if(!key.equals(loggerKey) &&
!key.equals(injectorKey) && !key.equals(stageKey)) {
binder.bind(key).toProvider(binding.getProvider());
}
}
}
};
}
I'm converting this to a proper parent/child relationship using:
Injector child = parent.createChildInjector(new OtherModules());
In order to test the old code worked properly, we had a test that created a
parent module in Stage.PRODUCTION and the fake child module in
Stage.PRODUCTION, and asserted that each singleton was properly eagerly
created. Since converting to using createChildInjector (and modifying the
test to using createChildInjector), it doesn't look like singletons are
being eagerly created anymore in the child.
The test is:
public class ModulesTest extends TestCase {
public void testChildEagerSingletons() {
Injector parent = Guice.createInjector(Stage.PRODUCTION, new
AbstractModule() {
@Override
protected void configure() {
bind(S1.class).to(S1I.class);
}
});
Injector child = parent.createChildInjector(new AbstractModule() {
@Override
protected void configure() {
bind(S2.class).to(S2I.class);
bind(S3I.class);
}
});
assertTrue(S1I.created);
assertSame(parent.getInstance(S1.class),
child.getInstance(S1.class));
assertTrue(S2I.created);
assertTrue(S3I.created);
}
private static interface S1 {}
@Singleton
private static class S1I implements S1 {
private static boolean created = false;
S1I () { created = true; }
}
private static interface S2 {}
@Singleton
private static class S2I implements S2 {
private static boolean created = false;
S2I () { created = true; }
}
@Singleton
private static class S3I {
private static boolean created = false;
S3I () { created = true; }
}
}
The assertTrue(S2I.created); fails.
Is this a bug, or expected behavior, or should I be doing something else?
Thanks!
Sam
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---