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"))) 

它在那里。 怎么了?

  • Android音频捕捉静音检测
  • 从Java调用Kotlin高阶函数
  • Spring的Facebook模板映射fetchObject到PagedList
  • 在Kotlin中的Lambdaexpression式
  • API 23(Marshmallow)的Android SDK是否支持Java 8?
  • Android Studio上的Kotlin(非Android)项目的ClassNotFoundException错误
  • 文件名,目录名称或卷标语法不正确 - Kotlin - Maven
  • Kotlin可以使用龙目吗?
  • 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 language will be the best programming language for Android.