6
votes

I have two named instances of a type bound 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 inject the instances directly, it must inject a provider to the instances:

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

The problem is that the FooPrime injection above is not injecting an instance named "prime", it's injecting a Provider named "prime", which of course is not what I want.

How do I tell Guice to inject a provider for the Foo instance named "prime"?

1
Is it misbehaving? This looks like it should work the way you want it to. - Jeff Bowman

1 Answers

7
votes

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