即使在使用限定符注释时,Dagger/DuplicateBindings 也具有通用接口

安德鲁

我有一个这样定义的基本接口:

@JvmSuppressWildcards
abstract class IWorkerContract<T, R>(
    private val mContext: Context,
    // Some other stuff
) {
    private val ctx: CoroutineScope = CoroutineScope(Job())

    // Some other stuff
}

现在,我有两个实现这个接口的类

class ShopItemProductDescWorkerContract @Inject constructor(
    // Some other stuff
) : IWorkerContract<@JvmSuppressWildcards List<ShopItemDescription>,@JvmSuppressWildcards String>( // important part
    // Some other stuff
){

class ShopItemTechnicalDescWorkerContract @Inject constructor(
    // Some other stuff
) : IWorkerContract<@JvmSuppressWildcards List<ShopItemDescription>,@JvmSuppressWildcards String>(
    // Some other stuff
){

最后,在我的模块中

@Module
@InstallIn(ViewModelComponent::class)
abstract class WorkerModule {
    @WorkerContract(EWorkerContracts.SHOP_ITEM_PROD)
    @Binds
    abstract fun bindShopItemProductDescWorkContract(sipContract: ShopItemProductDescWorkerContract): IWorkerContract<List<ShopItemDescription>, String>

    @WorkerContract(EWorkerContracts.SHOP_ITEM_TECH)
    @Binds
    abstract fun bindShopItemTechnicalDescWorkContract(sitContract: ShopItemTechnicalDescWorkerContract): IWorkerContract<List<ShopItemDescription>, String>

}

@Retention(AnnotationRetention.RUNTIME)
annotation class WorkerContract(val value: EWorkerContracts)

enum class EWorkerContracts {
    SHOP_ITEM_PROD,
    SHOP_ITEM_TECH
}

错误

error: [Dagger/DuplicateBindings] com.example.app.business.datasource.network.worker.list.IWorkerContract<java.util.List<com.example.app.business.domain.model.shop.item.ShopItemDescription>,java.lang.String> is bound multiple times:
  public abstract static class SingletonC implements App_GeneratedInjector,
                         ^
      @org.jetbrains.annotations.NotNull @Binds @com.example.app.di.business.network.WorkerContract(com.example.app.di.business.network.EWorkerContracts.SHOP_ITEM_PROD) com.example.app.business.datasource.network.worker.list.IWorkerContract<List<com.example.app.business.domain.model.shop.item.ShopItemDescription>,String> com.example.app.di.business.network.WorkerModule.bindShopItemProductDescWorkContract(com.example.app.business.datasource.network.worker.shop.item.productdesc.ShopItemProductDescWorkerContract)
      @org.jetbrains.annotations.NotNull @Binds @com.example.app.di.business.network.WorkerContract(com.example.app.di.business.network.EWorkerContracts.SHOP_ITEM_TECH) com.example.app.business.datasource.network.worker.list.IWorkerContract<List<com.example.app.business.domain.model.shop.item.ShopItemDescription>,String> com.example.app.di.business.network.WorkerModule.bindShopItemTechnicalDescWorkContract(com.example.app.business.datasource.network.worker.shop.item.technicaldesc.ShopItemTechnicalDescWorkerContract)
      com.example.app.business.datasource.network.worker.list.IWorkerContract<java.util.List<com.example.app.business.domain.model.shop.item.ShopItemDescription>,java.lang.String> is injected at
          com.example.app.business.interactor.shop.item.ShopItemInteractor(shopItemProductWorkerContract, �)
      com.example.app.business.interactor.shop.item.ShopItemInteractor is injected at
          com.example.app.presentation.shop.item.core.ShopItemViewModel(shopItemInteractor, �)
      com.example.app.presentation.shop.item.core.ShopItemViewModel is injected at
          com.example.app.presentation.shop.item.core.ShopItemViewModel_HiltModules.BindsModule.binds(vm)
      @dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at
          dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.example.app.presentation.App_HiltComponents.SingletonC ? com.example.app.presentation.App_HiltComponents.ActivityRetainedC ? com.example.app.presentation.App_HiltComponents.ViewModelC]
  It is also requested at:
      com.example.app.business.interactor.shop.item.ShopItemInteractor(�, shopItemTechnicalWorkerContract, �)
杰夫鲍曼

您没有@Named在示例中使用,@WorkerContract而是用作限定符 annotation(@Named是一个内置的限定符注释,但您可以像在您的示例中那样创建自己的注释。)

但是,您缺少@Qualifier元注释本身:

标识限定符注释。任何人都可以定义新的限定符。限定符注释:

  • @Qualifier@Retention(RUNTIME)、 和典型地注释@Documented
  • [...]

由于@Documented是可选的并且仅影响 Javadoc,请确保添加@Qualifier并且您会很高兴。

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class WorkerContract(val value: EWorkerContracts)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在具有通用参数的通用方法中使用Spring RestTemplate

具有反关系的通用接口

春天使用相同的接口与限定符太多的构造函数参数

使用Roaster,如何生成具有特定通用类型的接口?

实现Java接口时,确保“ Object o”参数具有相同的通用类型

具有通用接口的C#转换

返回具有通用类型的接口时出错

使用Castle Windsor解决具有通用类型约束的接口

具有接口的通用协方差-“ is”和“ =”运算符之间的怪异行为矛盾

具有通用接口参数的通用接口列表

使用LightInject注册实现通用接口的所有类

当接口具有多个通用参数时,如何解决“无法将[类型]隐式转换为[接口]”错误?

具有ref限定符的模板方法的重载解析

TypeScript通用接口,仅具有使用某些模板参数值定义的属性

将子接口用作具有通用接口的prop接口

在Moq中使用具有CQRS模式的通用接口

动态选择没有限定符注释的bean

具有通用类型的接口和方法

使用structuremap获取和/或弹出通用接口的所有实现

使用没有特定类型的通用接口?

具有某些接口类型的通用C#类未使用第一个接口的成员实现其他接口

为什么Spring不使用通用限定符进行注入?

DNS-具有不同限定符的多个SPF

自动装配模棱两可,但没有使用限定符

尝试使用子查询,但子查询返回多个带有限定符的行

在Android中为文件夹使用资源限定符时,我是否必须提供所有新资源?

不能对包含泛型的接口使用限定符

如何使用通用接口来增强具有附加值类型的现有接口?

使用接口与管理具有此接口的对象的通用类型类进行通信