Guice: inject different implementation depending on who is getting it?

Viewed 1483

I have two third-party classes, both of which take an implementation of an Authorizer interface. I need to inject each with a different implementation.

If I do an @Provides, how can I implement it so that it provides the implementation required at run time? The provider has no idea who is asking for the injection.

In theory I could use @Named, but I can't modify the code being injected. I want to do something like:

bind(Authorizer.class).to(ImplA.class).for(SomeClass.class)
bind(Authorizer.class).to(ImplB.class).for(SomeOtherClass.class)

Obviously, the "for" code doesn't exist, but is there some equivalent way to do this?

1 Answers
Related