单元测试多个依赖注入,而不使用Guice中的构造函数

时间:2018-01-15 06:08:38

标签: java guice

我写了一个类如下:

public class MyClass {
@Inject
@Named("MyMap1")
private Map<String, IEmployeeManager> myMap1;

@Inject
@Named("MyMap2")
private Map<String, ICustomerManager> myMap2;

@Inject
private ConfigManager configManager;

...
}

这个类没有构造函数。我无法弄清楚如何为此注入依赖项。在上面的代码中,我在模块中构建了两个地图绑定器MyMap1和MyMap2,如下所示:

public class ManagerConfigModule extends AbstractModule {
@Override
protected void configure() {

    MapBinder<String, IEmployeeManager> myMap1 = MapBinder.newMapBinder(binder(), String.class,
            EmployeeManager.class, Names.named("MyMap1"));

    myMap1.addBinding(SDEObj.class.getName()).to(SDManager.class)
            .asEagerSingleton();
    myMap1.addBinding(ASObj.class.getName()).to(VPManager.class)
            .asEagerSingleton();

    MapBinder<String, ICustomerManager> myMap2 = MapBinder.newMapBinder(binder(), String.class,
            CustomerManager.class, Names.named("MyMap2"));
    ...
 }

0 个答案:

没有答案