Rahul,
You can use Names.named("<str>") to create instances of the @Named
annotation. You can then use this to create a Key to make explicit
requests from an Injector.
You will need to change your bindings to include .annotatedWith() in the module:
bind(Inter.class).annotatedWith(Names.named("discriminator")).to(new Impl());
Key<Inter> key = Key.get(Inter.class, Names.named("discriminator"));
Binding<Inter> binding = injector.getBinding(key);
Inter specificImpl = injector.getInstance(binding);
Another, easier, way to do this would be to use multibindings in
guice2 and create a map of implementations.
-Dave
On Tue, Mar 31, 2009 at 10:46 AM, Rahul <[email protected]> wrote:
>
> Greetings,
>
> I'd would like to understand if @Named can be used programmatically to
> resolve Guice 'managed' instances. Here's the scenario:
>
> * There are a bunch of Command<REQ, RESP> instances bound in a
> module, say CommandModule:
>
> for example:
> bind(new TypeLiteral<ICommand<String, Movie>>() {
> }).to(new TypeLiteral<GetMovieDetailsCommand>() {
> });
>
> bind(new TypeLiteral<ICommand<String, Play>>() {
> }).to(new TypeLiteral<GetPlayDetailsCommand>() {
> });
>
> * A client can send a command invocation request that contains the
> Type of the Request, and Type of expected Response.
> GenericInvoker.invokeCommand(String.class, title,
> Movie.class);
>
> * A CommandResolver makes use of this info to resolve to an
> appropriate implementation.
>
> Problem:
> I would like to make use of @Named so that the CommandResolver
> implementation can resolve cases where two or more Command
> implementations can expect same Type of request and return same Type
> of response.
>
> Is it possible to make use to @Named programmatically by the
> CommandResolver implementation to differentiate between Command
> implementation that expect same Type of request and response.
>
> I would appreciate if there are any other/better ideas that this could
> be accomplished.
>
> Many thanks,
>
> Rahul
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---