Dagger Kotlin 限定符构造函数注入不起作用
Dagger Kotlin qualifier constructor injection doesn't work
我有以下模块使用带有限定符的@Provides 方法
@Module
class VocabularyModule {
@VocabularyProviders
@Singleton
@Provides
fun provideVocabularies(): List<VocabularyProvider> {
return listOf(
AnimalsVocabularyProvider(),
FamilyVocabularyProvider(),
FoodVocabularyProvider(),
NumberVocabularyProvider(),
ColorsVocabularyProvider(),
FreeTimeVocabularyProvider(),
SportVocabularyProvider(),
NatureVocabularyProvider(),
PeopleVocabularyProvider(),
TransportationVocabularyProvider()
)
}
}
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class VocabularyProviders
然后是我的 class,我想通过构造函数和限定符将此列表注入:
class VocabularyFactory
@Inject constructor(@param:VocabularyProviders val providers: List<VocabularyProvider>) {
fun getVocabulary(category: VocabularyCategory): Vocabulary {
for (provider in providers) {
if (category == provider.category) {
return provider.vocabulary
}
}
throw IllegalStateException("didn't find provider that could provide vocabulary of $category category")
}
}
我收到这个错误,但一切看起来都是正确的
11: error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] @cz.ejstn.learnlanguageapp.core.dagger.module.vocabulary.VocabularyProviders java.util.List<? extends cz.ejstn.learnlanguageapp.vocabulary.model.factory.VocabularyProvider> cannot be provided without an @Provides-annotated method.
我继续翻阅类似的问题并遇到了这个问题:
所以根据我的理解,kotlin 编译器 "messes a bit" 在参数中使用泛型类型,这导致 dagger 无法 link 这些我猜。
我像这样更改了工厂的构造函数以避免这种情况 - addind @JvmSuppressWildcards 注释:
class VocabularyFactory
@Inject constructor(@param:VocabularyProviders val providers:@JvmSuppressWildcards List<VocabularyProvider>) {
...
}
将此问题保留在这里,因为更多人可能 运行 进入此
我有以下模块使用带有限定符的@Provides 方法
@Module
class VocabularyModule {
@VocabularyProviders
@Singleton
@Provides
fun provideVocabularies(): List<VocabularyProvider> {
return listOf(
AnimalsVocabularyProvider(),
FamilyVocabularyProvider(),
FoodVocabularyProvider(),
NumberVocabularyProvider(),
ColorsVocabularyProvider(),
FreeTimeVocabularyProvider(),
SportVocabularyProvider(),
NatureVocabularyProvider(),
PeopleVocabularyProvider(),
TransportationVocabularyProvider()
)
}
}
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class VocabularyProviders
然后是我的 class,我想通过构造函数和限定符将此列表注入:
class VocabularyFactory
@Inject constructor(@param:VocabularyProviders val providers: List<VocabularyProvider>) {
fun getVocabulary(category: VocabularyCategory): Vocabulary {
for (provider in providers) {
if (category == provider.category) {
return provider.vocabulary
}
}
throw IllegalStateException("didn't find provider that could provide vocabulary of $category category")
}
}
我收到这个错误,但一切看起来都是正确的
11: error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] @cz.ejstn.learnlanguageapp.core.dagger.module.vocabulary.VocabularyProviders java.util.List<? extends cz.ejstn.learnlanguageapp.vocabulary.model.factory.VocabularyProvider> cannot be provided without an @Provides-annotated method.
我继续翻阅类似的问题并遇到了这个问题:
所以根据我的理解,kotlin 编译器 "messes a bit" 在参数中使用泛型类型,这导致 dagger 无法 link 这些我猜。
我像这样更改了工厂的构造函数以避免这种情况 - addind @JvmSuppressWildcards 注释:
class VocabularyFactory
@Inject constructor(@param:VocabularyProviders val providers:@JvmSuppressWildcards List<VocabularyProvider>) {
...
}
将此问题保留在这里,因为更多人可能 运行 进入此