Hello,

I am trying to bind the same class twice using annotations. This does
not work using explicit bindings, but it does using provides methods.
Seems like a bug to me. Here's a sample test class. The first test
fails because the same instance is returned. The second one passes as
expected. The class Foo is annotated w...@singleton.

public class GuiceMultiSingleton {

  @Test
  public void twoSingletonsWithExplicitBinding() {
    Injector injector = Guice.createInjector(new AbstractModule() {
      @Override
      protected void configure() {
 
bind(IFoo.class).annotatedWith(Names.named("a")).to(Foo.class);
 
bind(IFoo.class).annotatedWith(Names.named("b")).to(Foo.class);
      }
    });

    IFoo fooA = injector.getInstance(Key.get(IFoo.class,
Names.named("a")));
    IFoo fooB = injector.getInstance(Key.get(IFoo.class,
Names.named("b")));

    assertThat(fooA, not(sameInstance(fooB))); // fails, same instance
  }

  @Test
  public void twoSingletonsWithProvidesMethods() {
    Injector injector = Guice.createInjector(new AbstractModule() {
      @Override
      protected void configure() {
      }

      @Named("a")
      @Singleton
      @Provides
      IFoo provideFooA() {
        return new Foo();
      }

      @Named("b")
      @Singleton
      @Provides
      IFoo provideFooB() {
        return new Foo();
      }
    });

    IFoo fooA = injector.getInstance(Key.get(IFoo.class,
Names.named("a")));
    IFoo fooB = injector.getInstance(Key.get(IFoo.class,
Names.named("b")));

    assertThat(fooA, not(sameInstance(fooB))); // passes
  }
}


Am I missing something here?

Thanks,
Reinhard

-- 
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.

Reply via email to