java - Dagger2 - 应用程序不能依赖多个作用域组件
问题描述
注意这个问题可能与其他问题相似,但它提供了一个更好的解释和附件代码,旨在找到问题的解决方案,其他问题中提供的解决方案不适合。
就在几天前,我开始开发一个 Android 模块化应用程序。我使用 Dagger 2 来处理依赖注入,我目前正面临一个奇怪的行为。我有我的主应用程序模块和其他三个模块:
Core_Module
:它暴露了第三方库和存储层。Localisation_Module
:它公开了一个存储库以获取本地化信息。Configuration_Module
:它公开一个存储库以获取配置参数。
两者都Configuration_Module
依赖Localisation_Module
于Core_Module
.
核心组件:
@Singleton
@Component(modules = [ApplicationModule::class, NetworkingModule::class, RepositoryModule::class])
interface CoreComponent {
@Named("retrofit")
fun retrofit(): Retrofit
@Named("retrofitWithCache")
fun retrofitWithCache(): Retrofit
fun storageRepository(): StorageRepository
}
本地化组件:
@Component(modules = [ServiceModule::class, RepositoryModule::class], dependencies = [CoreComponent::class])
@LocalisationScope
interface LocalisationComponent {
fun localisationService(): LocalisationService
fun localisationRepository(): LocalisationRepository
}
配置组件
@Component(modules = [ServiceModule::class, RepositoryModule::class], dependencies = [CoreComponent::class])
@ConfigurationScope
interface ConfigurationComponent {
fun configurationService(): ConfigurationService
fun configurationRepository(): ConfigurationRepository
}
应用组件
@Component(dependencies = [LocalisationComponent::class, ConfigurationComponent::class])
@ApplicationScope
abstract class ApplicationComponent {
abstract fun inject(mainActivity: MainActivity)
}
主要应用
class MainApplication : Application() {
lateinit var applicationComponent: ApplicationComponent
override fun onCreate() {
super.onCreate()
this.registerDependencies()
}
private fun registerDependencies() {
val coreModule = CoreModule(applicationContext)
applicationComponent = DaggerApplicationComponent.builder()
.localisationComponent(LocalisationModule(coreModule).localisationComponent)
.configurationComponent(ConfigurationModule(coreModule).configurationComponent)
.build()
}
}
我决定设计这种架构是因为我想将功能分成独立的、可互换的模块,以便每个模块都包含执行特定功能并将单个模块导出到其他应用程序所需的一切。
不幸的是,我收到一条错误消息,指出不允许 Dagger 组件依赖于多个作用域组件。有谁知道如何面对这种问题?
解决方案
经过一整天的麻烦,我找到了适合我的情况的解决方案。所有描述的模块(Configuration_Module
,Localisation_Module
)都需要在我的应用程序模块中使用。
因此,对于被@ApplicationScope
@Module
调用ApplicationModule
,我删除了所有组件依赖项。
应用程序组件
@Component(modules = [ApplicationModule::class])
@ApplicationScope
abstract class ApplicationComponent {
abstract fun inject(mainActivity: MainActivity)
}
应用模块
@Module
class ApplicationModule(private val localisationComponent: LocalisationComponent,
private val configurationComponent: ConfigurationComponent) {
@Provides
@ApplicationScope
fun providesLocalisationRepository(): LocalisationRepository {
return localisationComponent.localisationRepository() // Provided by Localisation module with Dagger
}
@Provides
@ApplicationScope
fun providesConfigurationRepository(): ConfigurationRepository {
return configurationComponent.configurationRepository() // Provided by Configuration module with Dagger
}
}
最后是我的主要应用程序
class MainApplication : Application() {
lateinit var applicationComponent: ApplicationComponent
override fun onCreate() {
super.onCreate()
this.registerDependencies()
}
private fun registerDependencies() {
val coreModule = CoreModule(applicationContext)
val applicationModule = ApplicationModule(LocalisationModule(coreModule).localisationComponent,
ConfigurationModule(coreModule).configurationComponent)
applicationComponent = DaggerApplicationComponent.builder()
.applicationModule(applicationModule)
.build()
}
}
我指出,一切都像魅力一样运作。
推荐阅读
- python - 具有随机大小元素的随机大小的随机矩阵总计最大大小
- azure - Azure 数据目录 Gen2 是免费的吗?
- c++ - 如何访问 RTCVideoRenderer 的帧?(IOS)
- javascript - 如何在 JS 中以 YYYY-MM-DD 格式获取第二天的日期?
- python - 带有 Dynamodb 返回项的格式错误的节点或字符串
- ios - iOS上的中文文本转语音问题
- r - R中的download.file遗漏了内容
- amazon-web-services - AWS S3 复制 - 一个源存储桶到多个目标存储桶
- javascript - 如何在 React Native 中测量大量组件而不会导致错误?
- python - 链接到数据结构课程