Dependency injection 使用Guice+;科特林

Dependency injection 使用Guice+;科特林,dependency-injection,guice,kotlin,Dependency Injection,Guice,Kotlin,我正在Kotlin中使用以下控制器定义编写JavaFX应用程序: class MainController { @Inject private lateinit var componentDescriptors: List<ComponentDescriptor> /* More code goes here */ } 和多绑定扩展(切换列表以在corse的字段定义中设置): 但这两种方法都会导致相同的错误: No implementation for java

我正在Kotlin中使用以下控制器定义编写JavaFX应用程序:

class MainController {

    @Inject private lateinit var componentDescriptors: List<ComponentDescriptor>
    /* More code goes here */

}
和多绑定扩展(切换列表以在corse的字段定义中设置):

但这两种方法都会导致相同的错误:

No implementation for java.util.List<? extends simpleApp.ComponentDescriptor> was bound.
  while locating java.util.List<? extends simpleApp.ComponentDescriptor>
    for field at simpleApp.MainController.componentDescryptors(MainController.kt:6)
  while locating simpleApp.MainController

1 error
    at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1042)
    at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1001)
    at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1051)
    at com.gluonhq.ignite.guice.GuiceContext.getInstance(GuiceContext.java:46)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:929)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    ... 12 more

没有java.util.List的实现是的,它之所以发生是因为差异,但有一种方法可以让它工作

class MainController {
    @JvmSuppressWildcards
    @Inject
    private lateinit var componentDescriptors: List<ComponentDescriptor>    
}
class主控制器{
@JvmSuppressWildcards
@注入
私有lateinit var组件描述符:列表
}

默认情况下,Kotlin生成
列表@Michael给出了正确的解释。下面是一个用于单元测试
Set
multibinding的策略示例,适用于那些喜欢测试其模块的人:

class MyModuleTest {

  @JvmSuppressWildcards
  @Inject
  private lateinit var myTypes: Set<MyType>

  @Before fun before() {
    val injector = Guice.createInjector(MyModule())
    injector.injectMembers(this)
  }

  @Test fun multibindings() {
    assertNotNull(myTypes)
    assertTrue(myTypes.iterator().next() is MyType)
  }
}
类MyModuleTest{
@JvmSuppressWildcards
@注入
私有lateinit变量myTypes:Set
@在娱乐之前{
val injector=Guice.createInjector(MyModule())
注入器。注入器成员(本)
}
@测试多绑定(){
assertNotNull(myTypes)
assertTrue(myTypes.iterator().next()是MyType)
}
}

@Michael comment正在运行。如果要在构造函数中执行注入,需要执行以下操作

class MainController @Inject consturctor(
    private var componentDescriptors: List<@JvmSuppressWildcards ComponentDescriptor>  
) {}
class MainController@Inject constructor(
私有变量组件描述符:列表
) {}

有什么方法可以让这项工作为您服务吗?我试着将注释放在构造函数参数之前,在这里我想注入集合,但它不起作用。当我将
@jvmsuppresswidcards
注释放在类上时,它确实起作用,但我不想对每个类都这样做…@JanWytze您是否尝试将此注释应用于构造函数?
此注释不适用于目标“构造函数”
class MyModuleTest {

  @JvmSuppressWildcards
  @Inject
  private lateinit var myTypes: Set<MyType>

  @Before fun before() {
    val injector = Guice.createInjector(MyModule())
    injector.injectMembers(this)
  }

  @Test fun multibindings() {
    assertNotNull(myTypes)
    assertTrue(myTypes.iterator().next() is MyType)
  }
}
class MainController @Inject consturctor(
    private var componentDescriptors: List<@JvmSuppressWildcards ComponentDescriptor>  
) {}