guice multibinder与提供者 [英] guice multibinder with Providers

查看:127
本文介绍了guice multibinder与提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将其包含在我的代码中

I am trying to be able to have this in my code

@Inject
private Map<String, Provider<Processor>> providers;

我正在尝试,但是此代码无法编译

I was trying but this code does not compile

MapBinder<String, Provider<Processor>> mapbinder = MapBinder.newMapBinder(binder, String.class, Provider<Processor>.class);
mapbinder.addBinding("splineV1Beta").to(SplineProcessor.class);
mapbinder.addBinding("invertV1Beta").to(InvertProcessor.class);

此代码在启动时失败,因为它无法绑定我的地图

This code fails on startup in that it can't bind my Map

MapBinder<String,Processor> mapbinder = MapBinder.newMapBinder(binder, String.class, Processor.class);
mapbinder.addBinding("splineV1Beta").to(SplineProcessor.class);
mapbinder.addBinding("invertV1Beta").to(InvertProcessor.class);

如何在此处正确设置绑定?

How do I setup the bindings correctly here?

注意:我也在寻找简单的方法,因此开发人员每次使用新处理器时只需添加一行(我希望我不必添加一行+一些工厂接口...应该是一种方法,但除了TypeLiteral和toProvider()方法之外,我还尝试了其他方法.)

NOTE: I am looking for something easy too so developers just add one line every time we have a new processor.(I am hoping I don't have to add one line + some factory interface...should be a way, but I have tried other things in addition to above with TypeLiteral and toProvider() method as well).

更多信息:好的,我发现是否有下面的行(但是根本不调用addBinding)Guice实际上会启动,这很好,但是所有的addBinding().to方法签名现在都是错误的,因为它们想要

MORE INFO: okay, I found out if I have the below line(but do not call addBinding at all) Guice will actually startup which is good, but all the addBinding().to method signatures are now wrong as they want a

Provider<? extends Provider<Processor>>
//This below line ends up with mabbinder2.addBinder().to() wanting the above param type?
MapBinder<String, Provider<Processor>> mapbinder2 = MapBinder.newMapBinder(binder, stringLit, list);

彻底改变我的想法,我尝试每次注入这样的东西(希望map.get("xxx")每次都会创建新的实例...

RADICALLY change my thinking and I try injecting something like this(hoping that map.get("xxx") creates new instances every time...

@Inject
private Map<String, Processor> providers;

我这样绑定它,但是不幸的是,地图总是返回相同的实例:(...

and I bound it like this but unfortunately, the map is always returning the same instance :(...

MapBinder<String, Processor> mapbinder = MapBinder.newMapBinder(binder, String.class, Processor.class);
mapbinder.addBinding("splineV1Beta").toProvider(new TypeLiteral<Provider<SplineProcessor>>() {;});
mapbinder.addBinding("invertV1Beta").toProvider(new TypeLiteral<Provider<InvertProcessor>>() {;});

根据此文档

according to this doc http://google-guice.googlecode.com/svn/trunk/latest-javadoc/com/google/inject/multibindings/MapBinder.html you can have a

 MapBinder<String, Snack> and inject a Map<String, Provider<Snack>>

但是当我这样做(使用私有字段)时,我得到以下信息……(而当我更改为其他解决方案但不调用mapbinder.addBinding时,它可以绑定并正常工作).

but when I do that(with a private field), I get the following....(whereas when I change to my other solution but don't call mapbinder.addBinding, it binds and works just fine)...

1) No implementation for java.util.Map<java.lang.String,    
javax.inject.Provider<controllers.modules2.framework.Processor>> was bound.

我是否必须使用构造函数注入才能像他们的示例一样工作?我在一个抽象类中,因此更改10个类非常不便:(.

Do I have to use constructor injection for this to work like their example? I am in an abstract class so that would be very inconvenient to change 10 classes :(.

谢谢, 院长

推荐答案

尽管Guice似乎可以很好地互换使用JSR-330批注,但是Multibindings似乎将Provider类型隐藏在Map中,因此可能期望注入java.util.Map<java.lang.String, com.google.inject.Provider<...>>代替.我无法重现您的问题,但请尝试一下,看看是否有帮助.

Though Guice seems to be very good about using JSR-330 annotations interchangeably, it seems that Multibindings hides the Provider type within a Map and therefore may be expecting to inject a java.util.Map<java.lang.String, com.google.inject.Provider<...>> instead. I haven't been able to reproduce your problem, but try that and see if it helps.

侧面说明:如果要避免在任何地方的代码中对其进行更改,则可以将Map<String, javax.inject.Provider<Foo>>的提供程序与绑定了多绑定程序创建的Map<String, com.google.inject.Provider<Foo>>的内容绑定在一起.如果我是对的,这就是问题所在,则可以将其修复到一个位置,而不用在javax.injectcom.google.inject之间到处弹跳.

Side note: If you want to avoid changing it in code everywhere, you can hackishly bind a provider of Map<String, javax.inject.Provider<Foo>> to something that takes in the multibinder-created Map<String, com.google.inject.Provider<Foo>>. If I'm right and this is the problem, you can fix it one place rather than bouncing between javax.inject and com.google.inject everywhere.

这篇关于guice multibinder与提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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