Dagger2 Qualifier无法与Kotlin一起使用? [英] Dagger2 Qualifier not working with Kotlin?
问题描述
我有一个简单的课,如下所示
I have a simple class as below
class MainString(val msg: String)
我想注入不同的参数,所以我按照 https://google.github.io/dagger/users-guide
I want to inject with different argument to it, so I use the @Named
Qualifier as per the guide shown in https://google.github.io/dagger/users-guide
我的AppModule具有
With that my AppModule has
@Provides @Named("Two")
fun provideTwoMainString(): MainString {
return MainString("Two")
}
@Provides @Named("One")
fun provideOneMainString(): MainString {
return MainString("One")
}
然后在我的MainActivity中,我打电话
And in my MainActivity, I just call
@Inject @Named("One")
lateinit var stringOne: MainString
@Inject @Named("Two")
lateinit var stringTwo: MainString
但是,当我编译时,它会抱怨
However, when I compile, it complains
Error:(11, 1) error: com.elyeproj.demo_dagger_scope.MainString cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
似乎要我提供另一个没有限定符的提供者.因此,如果我添加以下内容,则所有内容都会编译.但这对我来说并不习惯,因为我想进行不同的论点注入.
It seems to want me to provide another Provider without the qualifier. So if I add the below, all will compiles. But it is not of used to me, as I want to have different argument injection.
@Provides
fun provideMainString(): MainString {
return MainString("Solo")
}
我做错了什么?
推荐答案
注释在kotlin上的工作略有不同.看起来此文档
Annotation work slightly different on kotlin. look this doc
您必须将字段注释为:
@Inject @field:Named("Two")
lateinit var stringOne: MainString
这篇关于Dagger2 Qualifier无法与Kotlin一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!