frjaeger220 / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

Child Injector Linked Singletons Not Eager #373

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
See the thread:
http://groups.google.com/group/google-guice/browse_thread/thread/32c683ff51c324d
3
.

Linked singletons in a child injector from a parent injector using
Stage.PRODUCTION are not eagerly instantiated.

Example test (from the email thread)

---

   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(S3I.created);
        assertTrue(S2I.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; }
    } 

---

This fails: assertTrue(S2I.created); 

Original issue reported on code.google.com by sberlin on 19 May 2009 at 1:12

GoogleCodeExporter commented 9 years ago
Fixed. Sam, please code review r996.
  http://code.google.com/p/google-guice/source/detail?r=996

Original comment by limpbizkit on 4 Jun 2009 at 7:35