The behavior here seems very strange.

bind(FooComponentImpl.class).annotatedWith(named("1")).in(Scopes.SINGLETON);

will fail (no implementation for FooComponentImpl annotated with
@Named(value=1) was bound).

So you need to write


bind(FooComponentImpl.class).annotatedWith(named("1")).to(FooComponentImpl.class).in(Scopes.SINGLETON);

This works fine UNLESS you also want a singleton scoped, unannotated
instance of FooComponentImpl:

   bind(FooComponentImpl.class).in(Scopes.SINGLETON);

If you do that, all the named bindings to FooComponentImpl will point to the
same object the unannotated binding does.

Colin

On Fri, Mar 26, 2010 at 2:41 PM, Chris Tucker <[email protected]> wrote:

> My expectation would be no (though on preview it looks like Willi might
> disagree, so a test seems in order!).  I think what you actually want is:
>
>
> bind(FooCompmentImpl.class).annotatedWith(Names.named("1")).in(Scopes.SINGLETON);
>
> bind(FooCompmentImpl.class).annotatedWith(Names.named("2")).in(Scopes.SINGLETON);
>
> bind(FooCompmentImpl.class).annotatedWith(Names.named("3")).in(Scopes.SINGLETON);
> bind(FooComponent.class).annotatedWith(Names.named("1").to(Key.get(FooComponentImpl.class,
> Names.named("1"));
> bind(FooComponent.class).annotatedWith(Names.named("2").to(Key.get(FooComponentImpl.class,
> Names.named("2"));
> bind(FooComponent.class).annotatedWith(Names.named("3").to(Key.get(FooComponentImpl.class,
> Names.named("3"));
>
> Otherwise you'll have an unannotated, unscoped binding of FooComponent to
> FooComponentImpl, along with three singleton-scoped bindings with different
> annotations for FooComponentImpl.  Attempting to inject an annotated
> FooComponent into your ModelImpl in that case will fail.
>
> Cheers,
> Chris
>
>
> On Fri, Mar 26, 2010 at 6:11 PM, Russ <[email protected]> wrote:
>
>> Thanks for the quick response Chris.
>>
>> What if ViewImpl needed multiple references to different
>> FooComponentImpl objects?  Do I annotate them to differentiate them?
>>
>> class ViewImpl extends ThirdPartyContainer
>>    implements View {
>>
>>     @Inject @Named("1") private FooComponentImpl _comp1;
>>    @Inject @Named("2") private FooComponentImpl _comp2;
>>    @Inject @Named("3") private FooComponentImpl _comp3;
>>
>>    void init() {
>>        // add() is a ThirdPartyFooComponent API method written
>>        // in terms of ThirdPartyFooComponent
>>         add(_comp1);
>>        add(_comp2);
>>        add(_comp3);
>>     }
>>
>> }
>>
>> bind(FooComponent.class).to(FooComponentImpl.class);
>>
>> bind(FooCompmentImpl.class).annotatedWith(Names.named("1")).in(Scopes.SINGLETON);
>>
>> bind(FooCompmentImpl.class).annotatedWith(Names.named("2")).in(Scopes.SINGLETON);
>>
>> bind(FooCompmentImpl.class).annotatedWith(Names.named("3")).in(Scopes.SINGLETON);
>>
>> Will ModelImpl get the right references?
>>
>> class ModelImpl implements Model {
>>
>>    @Inject @Named("1") private FooComponent _comp1;
>>    @Inject @Named("2") private FooComponent _comp2;
>>    @Inject @Named("3") private FooComponent _comp3;
>>
>> }
>>
>> --
>> 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]<google-guice%[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]<google-guice%[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.

Reply via email to