Actually you can bind a collection of these datasources. I had a similar issue a few months ago and I posted the question on StackOverflow. I think the answer may be useful to you. Here is the link:
http://stackoverflow.com/questions/6625837/generalize-guices-robot-legs-example-with-multibinding Basically, you need to have a global module. In it, you install your private modules then retrieve each exposed binding and you bind these into your map/multibinder. Regards, Olivier 2012/1/18 nino martinez wael <[email protected]> > Multibinder, sorry forgot to send the example last week. Ill do to morrow. > On Jan 17, 2012 2:40 PM, "Warren" <[email protected]> wrote: > >> I have an ItemService annotated with Names.named("dataSource1") and >> another one annotated with Names.named("dataSource2") exposed from two >> different Private Modules like this: >> >> >> ... new PrivateModule() >> { >> >> @Override >> protected void configure() >> { >> >> >> bind(ItemService.class).annotatedWith(Names.named("dataSource1")).to(ItemServiceImpl1.class); >> >> expose(ItemService.class).annotatedWith(Names.named("dataSource1")); >> } >> } >> >> ... new PrivateModule() >> { >> >> @Override >> protected void configure() >> { >> >> >> bind(ItemService.class).annotatedWith(Names.named("dataSource2")).to(ItemServiceImpl2.class); >> >> expose(ItemService.class).annotatedWith(Names.named("dataSource2")); >> } >> } >> >> Each exposed binding is of the same type, but has a different >> implementation. I want to be able to place theses in a map and inject >> the map so I can do something like this anywhere in my code: >> >> ItemService itemService = itemServices.get("dataSource1") >> >> I started to try and do this with MapBinder but ran into some problems >> and later found this at >> http://code.google.com/p/google-guice/wiki/Multibindings >> >> Limitations >> When you use PrivateModules with multibindings, all of the elements >> must be bound in the same environment. You cannot create collections >> whose elements span private modules. Otherwise injector creation will >> fail. >> >> What is the proper way to choose, at runtime, which binding to use >> when the bindings are exposed from 2 or more Private Modules ? >> >> Thanks, >> >> Warren >> >> -- >> 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. >> >> -- > 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. > -- Olivier Grégoire -- 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.
