java - 具有列表值的 Guice MapBinder

标签 java dependency-injection guice multimap

我有一项服务需要注入(inject) mulitmap - Map<String, List<Enricher>>

public class EnrichService {
    private Map<String, List<Enricher>> typeEnrichers;

    @Inject
    public EnrichService(Map<String, List<Enricher>> typeEnrichers) {
        this.typeEnrichers = typeEnrichers;
    }

    public void enrich(Entity entity) {
        List<Enricher> enrichers = typeEnrichers.get(entity.type);
        //.. enriching entity with enrichers
    }
}

class Entity {
    String id;
    String type = "shapedColorful";
    String color;
    String shape;
}

interface Enricher {
    void enrich(Entity entity);
}

class ColorEnricher implements Enricher {
    @Inject
    private  ColorService colorService;
    public void enrich(Entity entity) {
        entity.color = colorService.getColor(entity.id);
    }
}

class ShapeEnricher implements Enricher {
    @Inject
    private ShapeService shapeService;
    public void enrich(Entity entity) {            
        entity.shape = shapeService.getShape(entity.id);
    }
}

我需要有关在 juice 中配置 typeEnrichers Binder 的帮助 这是我正在尝试的,但卡住了

bind(ColorService).to(ColorServiceImpl.class);
bind(ShapeService).to(ShapeServiceImpl.class);
MapBinder<RelationType, List<Enricher>> mapBinder = MapBinder.newMapBinder(
            binder(), 
            new TypeLiteral<String>() {},
            new TypeLiteral<List<Enricher>>() {});

mapBinder.addBinding("shapedColorful", to(/*how to bind list of Enrichers here??*/))

任何帮助,我如何绑定(bind)这样的多图?

最佳答案

您正在尝试将 MapBinderMultibinder 混合在一起。

我建议您为每个 MapBinder 关系创建一个 Provider。实际上 Multibinder 本身就是一个 List Provider,具体来说,它的 RealMultibinder 实现不幸的是包私有(private)并且禁止使用。如果它不是包私有(private)的,也许我们可以这样使用它。很可能它无论如何都行不通......恕我直言,这会很好。

bind(ColorService).to(ColorServiceImpl.class);
bind(ShapeService).to(ShapeServiceImpl.class);
MapBinder<RelationType, List<Enricher>> mapBinder = MapBinder.newMapBinder(
            binder(), 
            new TypeLiteral<String>() {},
            new TypeLiteral<List<Enricher>>() {});

mapBinder.addBinding("shapedColorful", toProvider(Multibinder.newSetBinder(this.binder(), Enricher.class).addBinding().to(ColorService.class).addBinding().to(ShapeService.class).asEagerSingleton()))

您仍然可以创建并使用提供程序:

public class ShapeColorfulProvider implements Provider<List<Enricher>> {
 @Inject private ColorService colorService;
 @Inject private ShapeService shapeService;

 public List<Enricher> get() {
   return Lists.newArrayList(colorService,shapeService);
 }

}

然后

   mapBinder.addBinding("shapedColorful", toProvider(ShapeColorfulProvider.class))

关于java - 具有列表值的 Guice MapBinder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49107121/

相关文章:

java - 如何在 Clojure 中创建命令行可执行文件

java - 如何使用十进制格式使 0 显示为 0.00?

php - 在运行时使用 env() 在 Symfony 3.2 中加载外部环境参数返回未解析的值

jsf-2 - JSF2.0 Tomcat7中@Named和@ManagedBean注解的区别

java - Guice:在没有 @Singleton 或以其他方式修改实现的情况下配置单例

java - RoboGuice wiki 过时了吗? setBaseApplicationInjector 不存在?

dependency-injection - 如何正确地使用深度对象图和许多依赖项进行手动 DI

java - Eclipse Java 模板添加扩展或抛出类名

java - selenium + java - 如何下载 pdf 并用不同的名称保存?

c# - 如何使用依赖注入(inject)将状态传递到依赖链中