Tag: 吉斯

注入一个实例Kotlin&Guice失败

我有以下代码: class Test { private val context = Context().apply { property = “foo” } private val injector = Guice.createInjector(Module { it.bind(Context::class.java).toInstance(context) }) @Test fun `service received correct context`() { assertThat(injector.getInstance(Service::class.java).context.property, equalTo(“foo”)) } } class Service @Inject constructor(val context: Context) class Context { @Inject lateinit var property: String } 当运行它时,测试失败,抱怨(正确)一个空字符串不是“foo”。但为什么Guice不使用我精心创建的实例,并以Moduleforms提供给注入器? (这是第一个问题。) 但是,如果我更改Context看起来像这样: class Context { @get:Inject […]

在Kotlin&Guice中提供一个通用实例

我有一个Guice Module提供一个List使用@Provides方法。 class TestModule() : Module { override fun configure(binder: Binder) {} @Provides fun getStrings(): List = listOf(“foo”, “bar”) } class Test { @Test fun `provider can not deliver`() { val injector = Guice.createInjector(TestModule()) injector.getInstance(object : Key<List>() {}) } } 但是,测试失败: 1) No implementation for java.util.List was bound. while locating java.util.List 现在,这似乎是这个问题相同,但我不知道在哪里添加@JvmSuppressWildcards注释; 将它添加到getStrings()方法不会改变任何东西,就像在getInstance()调用中将其添加到object 。 我如何让Guice做我想做的事?