@Named default implementation

150 views
Skip to first unread message

David Garcia

unread,
Jun 2, 2011, 7:49:32 PM6/2/11
to google-guice
I have different implementations bound to an interface using @Named,
how can i get a "default" implementation when none of the bound
implementations match?

For example:

configure() {

bind(Thing.class).annotatedWith(Names.named("first")).to(FirstThing.class);

bind(Thing.class).annotatedWith(Names.named("second")).to(SecondThing.class);
}


if somewhere in the code there is something like:

@Inject @Named("third") Thing thirdThing;


Is there any way to return a default, say FirstThing.class, instead of
"third" in case it's not bound???

Bob Lee

unread,
Jun 2, 2011, 8:29:57 PM6/2/11
to google...@googlegroups.com
Does this work?

bind(Thing.class).annotatedWith(Named.class).to(DefaultThing.class);

Bob


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.


David Garcia

unread,
Jun 6, 2011, 1:45:57 PM6/6/11
to google-guice
It does, thanks!

On Jun 2, 5:29 pm, Bob Lee <crazy...@crazybob.org> wrote:
> Does this work?
>
> bind(Thing.class).annotatedWith(Named.class).to(DefaultThing.class);
>
> Bob
>
Reply all
Reply to author
Forward
0 new messages