Guice MapBinder

我在Java + Kotlin项目中使用Guice将算法实现的映射注入到特定的bean中,但是我一直在获取

1) No implementation for java.util.Map<java.lang.String, ? extends com.scherule.scheduling.algorithms.SchedulingAlgorithm> annotated with @com.google.inject.name.Named(value=scheduling.algorithms) was bound. while locating java.util.Map<java.lang.String, ? extends com.scherule.scheduling.algorithms.SchedulingAlgorithm> annotated with @com.google.inject.name.Named(value=scheduling.algorithms) for the 2nd parameter of com.scherule.scheduling.SchedulingJobConsumer.<init>(SchedulingJobConsumer.kt:17) while locating com.scherule.scheduling.SchedulingJobConsumer 

模块的定义是这样定义的:

 class AlgorithmsModule : AbstractModule() { override fun configure() { MapBinder.newMapBinder(binder(), String::class.java, SchedulingAlgorithm::class.java, Names.named("scheduling.algorithms") ).addBinding("interval-projection").toInstance(IntervalProjectionAlgorithm()) } } 

而来电号码是:

 @Singleton class SchedulingJobConsumer @Inject constructor( @Named("scheduling.channel") channel: Channel, @Named("scheduling.algorithms") val schedulers: Map<String, SchedulingAlgorithm> ) : DefaultConsumer(channel) ... 

但是当我这样做

 injector.getInstance(Key.get(new TypeLiteral<Map<String, SchedulingAlgorithm>>() {}, named("scheduling.algorithms"))) 

它在那里。 怎么了?

One Solution collect form web for “Guice MapBinder”

问题是Kotlin Generics!= Java泛型。

这Kotlin代码。

  @Named("scheduling.algorithms") val schedulers: Map<String, SchedulingAlgorithm> 

获取编译为

 Map<String, ? extends SchedulingAlgorithm> and not Map<String, SchedulingAlgorithm> 

当注射Guice正在寻找Map<String, ? extends SchedulingAlgorithm> ,? Map<String, ? extends SchedulingAlgorithm> ,因为MapBinderMap<String, SchedulingAlgorithm>创建了绑定,所以它没有绑定。

  • 了解Kotlin的特质
  • 如何通过自定义的webView取代webView?
  • 在android中无法使用dismiss方法解除Progress对话框
  • Kotlin 0.11.91未解决参考:inv()
  • 在gradle kotlin中访问任务的属性
  • 如何确定一个对象是否是从Kotlin中的某个类继承的?
  • 麻烦解决anko库版本0.9.1
  • 泛化参数和返回类型的泛化方法
  • Kotlin数据类复制与var值更改
  • 如何转换Array <T?>? 到Kotlin的Array <T>中
  • 如何写while循环与在kotlin中的responseInputStream.read - (while((i = responseInputStream.read(byteContainer))
  • Kotlin language will be the best programming language for Android.