Open GoogleCodeExporter opened 9 years ago
class "A" should actually read:
@Singleton class A { @Inject A(Provider<B> b) {} }
(otherwise B is constructed as a part of A and there's no issue)
Original comment by sberlin
on 11 Jun 2009 at 10:53
Here's a full test that outlines four different scenarios. Each of these pass
when
the Injector is the parent injector, but in a child injector the last two fail.
---
package com.google.inject;
import junit.framework.TestCase;
public class ChildInjectorTest extends TestCase {
@Override
protected void setUp() throws Exception {
AImpl.count = 0;
}
public void testExplicitEagerSingleton() {
Guice.createInjector(Stage.PRODUCTION).createChildInjector(new AbstractModule() {
@Override
protected void configure() {
bind(AImpl.class);
}
});
assertEquals(1, AImpl.count);
}
public void testLinkedEagerSingleton() {
Guice.createInjector(Stage.PRODUCTION).createChildInjector(new AbstractModule() {
@Override
protected void configure() {
bind(AInter.class).to(AImpl.class);
}
});
assertEquals(1, AImpl.count);
}
public void testImplicitEagerSingletonThroughSingleton() {
Guice.createInjector(Stage.PRODUCTION).createChildInjector(new AbstractModule() {
@Override
protected void configure() {
bind(B.class).in(Scopes.SINGLETON);
}
});
assertEquals(1, AImpl.count);
}
public void testImplicitEagerSingletonThroughUnscoped() {
Guice.createInjector(Stage.PRODUCTION).createChildInjector(new AbstractModule() {
@Override
protected void configure() {
bind(B.class);
}
});
assertEquals(1, AImpl.count);
}
@Singleton
private static class AImpl implements AInter {
static int count;
AImpl() { count++; }
}
private static interface AInter {}
private static class B { @Inject B(Provider<AImpl> a) {} }
}
Original comment by sberlin
on 12 Jun 2009 at 2:49
patch @ issue 387 solves this particular problem by listening to bindings even
if
they're discovered after injection completes in order to create eager
singletons.
Original comment by sberlin
on 12 Jun 2009 at 3:52
Original issue reported on code.google.com by
sberlin
on 11 Jun 2009 at 10:51