The reason you are seeing this is because of the way the "bind"
statement works. When you say "bind(Bar.class).to(Foo.class);",
you're telling Guice that you want Bar to be whatever Foo is bound to.
So, even if you had an additional "bind(Bazz.class).to(Foo.class);",
all that would do is tell Guice to link Bazz to whatever Foo is bound
to. If Foo is a Singleton (either by an explicit
bind(Foo.class).in(Scopes.SINGLETON) or by adding @Singleton to Foo),
then both Bar & Bazz are going to ultimately be the same instance,
because they're both linked to Foo's binding, and Foo is a singleton.
This is different than what you're seeing in the @Provides methods,
because you're explicitly constructing a new Foo new in each method
there.
You can get the behavior by explicitly binding two different Foo singletons.
Add two explicit different bindings for Foo & link to them explicitly:
bind(Foo.class).annotatedWith(Names.named("a"));
bind(Foo.class).annotatedWith(Names.named("b"));
bind(Bar.class).to(Keys.get(Names.named("a"),
Foo.class)).in(Scopes.SINGLETON);
bind(Bazz.class).to(Keys.get(Names.named("b"),
Foo.class)).in(Scopes.SINGLETON);
Or, if you prefer using @Provides methods:
bind(Foo.class).annotatedWith(Names.named("a"));
bind(Foo.class).annotatedWith(Names.named("b"));
@Provides @Singleton Bar barFoo(@Named("a") Foo foo) { return foo; }
@Provides @Singleton Bazz bazzFoo(@Named("b") Foo foo) { return foo; }
The difference between these examples and the code you provided is
that the linked bindings here are linking each to a different
explicitly bound Foo.
Sam
On Thu, Feb 25, 2010 at 8:07 AM, unguiculus <[email protected]> wrote:
> 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.
>
>
--
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.