我在https://google.github.io/dagger/multibindings 中使用了示例
模块 A
@Module
object MyModuleA {
@Provides
@IntoSet
fun provideOneString(): String {
return "ABC"
}
}
Run Code Online (Sandbox Code Playgroud)
模块 B
@Module
object MyModuleB {
@Provides
@ElementsIntoSet
fun provideSomeStrings(): Set<String> {
return HashSet<String>(Arrays.asList("DEF", "GHI"))
}
}
Run Code Online (Sandbox Code Playgroud)
成分
@Component(modules = [ MyModuleA::class, MyModuleB::class])
interface MyComponent {
fun strings(): Set<String>
}
Run Code Online (Sandbox Code Playgroud)
测试
@Test
fun testMyComponent() {
val myComponent = DaggerMyComponent.builder().build()
println("${myComponent.strings()}")
}
Run Code Online (Sandbox Code Playgroud)
它显示必须设置 MyModuleA 的错误,但将模块从对象更改为类工作正常。
@Module
class MyModuleA {
@Provides
@IntoSet
fun provideOneString(): String {
return "ABC"
}
}
@Module
class MyModuleB {
@Provides
@ElementsIntoSet
fun provideSomeStrings(): Set<String> {
return HashSet<String>(Arrays.asList("DEF", "GHI"))
}
}
Run Code Online (Sandbox Code Playgroud)
@IntoSet 注释在 kotlin 对象中不起作用?
| 归档时间: |
|
| 查看次数: |
1428 次 |
| 最近记录: |