相关组件中的Dagger2和限定符 [英] Dagger2 and qualifiers in dependent components
问题描述
我有一个应用程序组件和一个从属组件.应用程序组件声明显式依赖项,并且依赖项组件可以注入这些依赖项.但是,当我有一个必须与@Qualifier消除歧义的依赖项时,该依赖项组件将无法注入该依赖项.
I have an app component and a dependent component. The app component declares explicit dependencies, and the dependent component can inject those. However, when I have a dependency that I have to disambiguate with a @Qualifier, the dependent component is not able to inject that dependency.
这是应用程序组件
@Component(modules = [AppModule::class, SchedulersModule::class, StorageModule::class])
@ApplicationScope
interface AppComponent {
fun inject(app: Application)
/* other stuff omitted for brevity */
val bitmapCache: BitmapCache
@UiScheduler fun uiScheduler(): Scheduler
}
这是调度程序模块:
@Module
class SchedulersModule {
@ApplicationScope
@Provides
@IoScheduler
fun provideIoScheduler(): Scheduler = Schedulers.io()
@ApplicationScope
@Provides
@UiScheduler
fun provideMainThreadScheduler(): Scheduler = AndroidSchedulers.mainThread()
}
这是预选赛:
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class UiScheduler
这是从属组件:
@Component(
dependencies = [AppComponent::class],
modules = [EditEntryActivityModule::class, ViewModelModule::class]
)
@ActivityScope
interface EditEntryActivityComponent {
fun inject(editEntryActivity: EditEntryActivity)
fun inject(editEntryFragment: EditEntryFragment)
}
这是将调度程序注入片段的方式:
This is how the scheduler is injected in the fragment:
class EditEntryFragment : Fragment() {
@Inject @UiScheduler lateinit var uiScheduler: Scheduler
/* other stuff */
}
那么为什么从属组件可以注入在父组件中声明的位图缓存,而不是UI调度程序?这是我得到的错误:
So why can the dependent component inject the bitmap cache, declared in the parent component, but not the UI scheduler? This is the error I get:
error: io.reactivex.Scheduler cannot be provided without an @Provides- or @Produces-annotated method.
io.reactivex.Scheduler is injected at
com.test.edit.EditEntryFragment.uiScheduler
com.test.edit.EditEntryFragment is injected at
com.test.edit.EditEntryActivityComponent.inject(arg0)
1 error
推荐答案
在类EditEntryFragment中使用@field:UiScheduler
Using @field:UiScheduler in class EditEntryFragment
这篇关于相关组件中的Dagger2和限定符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!