I'm trying to inject an ArrayList
of String
s with the help of Guice. I want to show a panel with many RadioButtons (for example) where an user can select some services to activate.
Once selected, I would like to get all the names of the selected services and add them into a list, and inject this list to the manager responsible to create the services. Here is an example:
public class UIProviderModule extends ProviderServiceModule {
private ArrayList<String> requestedServices;
public UIProviderModule(ArrayList<String> requestedServices) {
this.requestedServices = requestedServices;
}
@Override
protected void configure() {
bindConstant().annotatedWith(Names.named(Constants.REQ_SERVICES)).to(requestedServices);
bind(IParser.class).to(UIParser.class);
super.configure();
}
}
I've seen many posts about Multibindings and also about Providers, but I did not understand how this could help me. I just want to retrieve names, since I'm not working with classes that have to be bound to an interface. Am I missing something?
Note: I know this is maybe not the good way to use Guice because I'm giving the list to be bound to the Module
.
I think you are misunderstanding how modules are supposed to work.
Modules don't create the objects, modules define rules for how objects might be created when they are needed.
The reason MapBinder would help is that you would define all of the services in your radio buttons list, and then use the injected map to activate the services that you need.
Here's some code to illustrate what I mean:
Then, inject the MapBinder to your
ServiceManager
class - which is not a module: