I have the following code using Guice bindings:
public class MyApplication { public static void main(String[] args) { Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(Foo.class).annotatedWith(Names.named("first")).toInstance(new Foo("firstFoo")); bind(Foo.class).annotatedWith(Names.named("second")).toInstance(new Foo("secondFoo")); bind(Bar.class).to(BarImpl.class); bind(MyApplication.class).asEagerSingleton(); } }); } private @Named("first") Bar first; private @Named("second") Bar second; static @Value class Foo { String name; } static interface Bar {} static class BarImpl implements Bar { @Inject @Named Foo foo; } } I'm trying to get a Bar object for both named Foos injected in my application. Basically, it should somehow connect the @Named on Foo with the one on Bar. I have tried several solutions, from putting @Named on everything to writing a custom Provider. The latter didn't work because I don't have access to the value of the @Named annotation inside the provider. I think the solution is somewhere in the line bind(Bar.class).to(BarImpl.class);, telling it to remember the value of the @Named annotation.
My question is, is this possible at all, and if so, how?