Guice:注入一个字符串的ArrayList [英] Guice : Inject an ArrayList of Strings

查看:168
本文介绍了Guice:注入一个字符串的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在Guice的帮助下注入一个 ArrayList String 。我想显示一个带有许多RadioButtons的面板(例如),用户可以在其中选择一些要激活的服务。



一旦选中,我想得到所有的名字选定的服务并将它们添加到列表中,并将此列表注入负责创建服务的经理。下面是一个示例:

  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();
}

}

我看过很多关于 Multibindings 以及提供商,但我不明白这对我有何帮助。我只想检索名称,因为我不使用必须绑定到接口的类。我错过了什么吗?



注意:我知道这可能不是使用Guice的好方法因为我给的列表是绑定到模块

解决方案

我认为你误解了模块是怎样的应该工作。



模块不创建对象,模块定义如何创建对象的规则需要。



原因 MapBinder 将有助于您在单选按钮列表中定义所有服务,然后使用注入映射以激活您需要的服务。



这里有一些代码来说明我的意思:

 公共类ServiceModule扩展AbstractModule {
protected void configure(){
MapBinder< String ,服务> mapbinder
= MapBinder.newMapBinder(binder(),String.class,Service.class);
mapbinder.addBinding(service1)。to(Service1.class).in(Singleton.class);
mapbinder.addBinding(service2)。to(Service2.class);
//在此定义所有服务,而不仅仅是正在使用的服务。
//如果你想要
}
}


然后,将MapBinder注入 ServiceManager 类 - 这是模块:

 公共类ServiceManager {
private final Map< String,Service> serviceMap;

@Inject
public ServiceManager(Map< String,Service)serviceMap){
this.serviceMap = serviceMap;
}

//这只是一种方法。这取决于你的UI如何工作
public void startAll(List< String> serviceList){
for(String serviceName:serviceList){
serviceMap.get(serviceName).start();
}
}
}


I'm trying to inject an ArrayList of Strings 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:

public class ServiceModule extends AbstractModule {
  protected void configure() {
    MapBinder<String, Service> mapbinder
        = MapBinder.newMapBinder(binder(), String.class, Service.class);
    mapbinder.addBinding("service1").to(Service1.class).in(Singleton.class);
    mapbinder.addBinding("service2").to(Service2.class);
    // Define ALL the services here, not just the ones being used.
    // You could also look this up from a ClassLoader or read from a configuration file if you want
  }
}

Then, inject the MapBinder to your ServiceManager class - which is not a module:

public class ServiceManager {
  private final Map<String, Service> serviceMap;

  @Inject
  public ServiceManager(Map<String, Service) serviceMap) {
    this.serviceMap = serviceMap;
  }

  // This is just one way to do it. It depends on how your UI works
  public void startAll(List<String> serviceList) {
    for(String serviceName : serviceList) {
      serviceMap.get(serviceName).start();
    }
  }
}

这篇关于Guice:注入一个字符串的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆