Using personalized annotations with Guice providers

I have two named instances of type binding in my application:

bind(Foo.class).toProvider(FooProvider.class); bind(Foo.class).annotatedWith(Names.named("prime")).toProvider(FooPrimeProvider.class); 

I have a class that would like to use one instance of each. For technical reasons, this class cannot insert instances directly; it must insert a provider into instances:

 class Bar { @Inject static Provider<Foo> fooProvider; @Inject @Named("prime") static Provider<Foo> fooPrimeProvider; // WRONG! } 

The problem is that the FooPrime injection above does not inject an instance named "prime", it does introduce a provider named "prime", which, of course, is not what I want.

How can I say that Guice introduces a provider for the Foo instance named "prime"?

+4
source share
1 answer

I just wrote a test, it does exactly what you want: https://gist.github.com/jangalinski/4943871

+5
source

All Articles