xiaodududu / google-guice

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

potential binding resolution order break in childInjectors #588

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
per:
  http://code.google.com/p/google-guice/wiki/BindingResolution

I'm thinking that the final assertion in the test below would be true.  But the 
child injector returns an instance of MemberInjectedClass that has been 
injected with "DefaultImplementation", not "CustomImplementation."  Perhaps 
this is working as intended, and I suspect so, given that the binding in the 
parent injector for "MemberInjectedClass" might itself be memoized in the 
parent's configuration.

Filing as a potential non-issue, with aim for clarification.

public class ChildInjectorTest extends TestCase {

  @ImplementedBy(DefaultImplementation.class)
  interface TestInterface { }

  static class DefaultImplementation implements TestInterface { }
  static class CustomImplementation implements TestInterface{ }

  static class MemberInjectedClass {
    @Inject TestInterface testInterface;
  }

  static Module getCustomModule() {
    return new AbstractModule() {
      @Override protected void configure() {
        bind(TestInterface.class).to(CustomImplementation.class);
      }
    };
  }

  public void testCreateChildInjector() {
    Injector parentInjector = Guice.createInjector(getCustomModule());
    MemberInjectedClass instance1 =
        parentInjector.getInstance(MemberInjectedClass.class);

    Injector childInjector = Guice.createInjector();
    MemberInjectedClass instance2 =
        childInjector.getInstance(MemberInjectedClass.class);

    assertSame(instance1.testInterface.getClass(), CustomImplementation.class);
    assertSame(instance2.testInterface.getClass(), CustomImplementation.class);

  }
}

Original issue reported on code.google.com by ffa...@gmail.com on 13 Jan 2011 at 8:19

GoogleCodeExporter commented 9 years ago
I think this is a result of a typo.

The line:
 Injector childInjector = Guice.createInjector();

should read:
 Injector childInjector = parentInjector.createChildInjector();

If it does, the test should pass.

Original comment by sberlin on 14 Jan 2011 at 9:36

GoogleCodeExporter commented 9 years ago
totally, thanks.

Original comment by ffa...@gmail.com on 15 Jan 2011 at 2:17