Guice injector has ... array as an argument:
Guice.createInjector(Module... modules);
So you can simply do:
Guice.createInjector(new ExchangeModule(), new RealmModule());
If there are some conflicts you can try binding annotations or untargetted bindings.
EDIT:
This might show how you could avoid conflicts:
bind(NetworkService.class).annotatedWith(Exchange.class).to(ExchangeManager.class);
bind(NetworkService.class).annotatedWith(Realm.class).to(RealmManager.class);
then:
@Inject
@Exchange
private NetworkService exchangeService;
EDIT 2:
I think the greatest problem is the way you initiate the NetworkService. You can make it an abstract class and then write something like that:
public class ExchangeHandler extends AbstractHandler {
@Inject
ExchangeHandler(@Exchange NetworkService service) {
super(service);
}
}
You want to have 2 distinct instances that use the exactly same code except for obtaining the NetworkService instance. There are several ways to do it but this one seems to be the most readable at the moment. Alternatively you might consider using Inversion of Control to pass NetworkService you need at the moment instead of creating 2 distinct trees of objects for Exchange
and Reaml
managers.
class ClassUsingManagers {
@Inject
ClassUsingManagers(
@Exchange NetworkService exchangeService,
@Realm NetworkService realmService,
NetworkServiceHandler handler) {
handler.actionForService(exchangeService);
handler.actionForService(realmService);
}
}
or if you just want to initiate them and run one method (which initialized everything else):
Multibinder serviceSet = Multibinder.newSetBinder(binder(), NetworkService.class);
serviceSet.addBinding().asEagerSingleton();
an then:
class ClassUsingManagers {
@Inject
ClassUsingManagers(
Set<NetworkService> services,
NetworkServiceHandler handler) {
for(NetworkService service : services)
handler.actionForService(service);
}
}
That other method would require you to install Guice Multbinding addon to Guice.