On Fri, Mar 26, 2010 at 7:07 PM, Russ <[email protected]> wrote:

> >
> bind(FooComponentImpl.class).annotatedWith(named("1")).in(Scopes.SINGLETON);
> >
> > will fail (no implementation for FooComponentImpl annotated with
> > @Named(value=1) was bound).
>
> Yes, exactly what I see in my test.
>
> > So you need to write
> >
> >
> bind(FooComponentImpl.class).annotatedWith(named("1")).to(FooComponentImpl.class).in(Scopes.SINGLETON);
>
> Don't you mean
>
>
> bind(FooComponent.class).annotatedWith(named("1")).to(FooComponentImpl.class).in(Scopes.SINGLETON);
>
> Else how does Guice resolve requests for FooComponent?


You want to use FooComponentImpl.class in both cases (sorry for the mistake
in my earlier example).  You're binding the key <FooComponentImpl,
@Named("1")> in singleton scope to FooComponentImpl (in this case,
<FooComponentImpl, @Named("1")> is the binding source, and FooComponentImpl
is the binding target).

You also need the second block of bindings to bind keys for FooComponent to
FooComponentImpl.  These bind keys of the form <FooComponent, @Named("1")>
to the bindings for <FooComponentImpl, @Named("1")>, which will inject the
singleton-scoped instances you want.  Without them you won't be able to
inject @Named("1") FooComponent foo; as there won't be a binding for
FooComponent annotated with @Named("1").


> > 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.
>
> I don't think I want, in fact want to prevent, unscoped instances of
> FooComponentImpl being injected.
>

There's probably some way of doing this (ensuring that a no-scope,
unannotated instance of a concrete class can't be injected), but without
access to my normal machine I can't test anything out.

Cheers,
Chris


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