简体   繁体   English

在 Dagger2 中使用 Kotlin 进行多重绑定

[英]Multi Binding In Dagger2 with Kotlin

So, I have this issue which I think I understand what's happening but I don't know a way out.所以,我有这个问题,我想我明白发生了什么,但我不知道出路。 Note: I'm pretty new to Dagger 2...I'm coming from SpringBoot and it pretty lazy out there, so take it easy on me.注意:我对 Dagger 2 还很陌生……我来自 SpringBoot,它很懒惰,所以请放轻松。

I have a module that provides three methods, the last method depends on the first two methods like below:我有一个提供三种方法的模块,最后一种方法取决于前两种方法,如下所示:

@Module
class CoreModule {

    @Provides
    @Named("appName")
    fun appName() : String{
        return "Uburu Store"
    }

    @Provides
    @Named("appVersion")
    fun appVersion() : String {
        return "0.0.1"
    }

    @Provides
    @Named("appInfo")
    fun appInfo(appName: String, appVersion: String) : String {
        return "$appName Version:: $appVersion"
    }
}

I'm getting an error that:我收到一个错误:

[Dagger/MissingBinding] java.lang.String cannot be provided without an @Inject constructor or an @Provides-annotated method.

My ApplicationComponent class:我的应用程序组件 class:

@Component(modules = [(CoreModule::class), (AndroidSupportInjectionModule::class), (ActivityBuildersModule::class)])
interface ApplicationComponent : AndroidInjector<ApplicationClass> {

    fun inject(application: Application)

    @Component.Builder
    interface Builder {

        @BindsInstance
        fun application(application: Application?): Builder?

        fun build(): ApplicationComponent?
    }

}

My activity class:我的活动 class:

class MainActivity : DaggerAppCompatActivity() {

    @Inject @Named("appInfo") lateinit var appInfo : String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Toast.makeText(this, appInfo, Toast.LENGTH_LONG).show()
    }
}

I managed to make it work if I don't use @Named annotation in any of the two dependencies ie if I delete one of the Two provided dependencies from CoreModule, and remove the @Named annotation from the only dependency left, it will work fine like below:如果我不在两个依赖项中的任何一个中使用@Named 注释,我设法使它工作,即如果我从CoreModule 中删除两个提供的依赖项之一,并从剩下的唯一依赖项中删除@Named 注释,它将正常工作如下所示:

    @Module
    class CoreModule {
    
        @Provides
        fun appName() : String{
            return "Uburu Store"
        }
    
        @Provides
        @Named("appInfo")
        fun appInfo(appName: String) : String {
            return "$appName Version:: $"
        }
    
    }
//This will work just fine, which made me realize that the @Named must be making it difficult for Dagger to understand something.

The problem with this approach is that I can't do without the @Named as it's needed for Multiple Binding of the same DataTypes eg: String.这种方法的问题是我不能没有@Named,因为它是相同数据类型的多重绑定所必需的,例如:字符串。

My research so far made it known to me that I have to use @JvmSuppressWildcards somewhere but I'm not sure where.到目前为止,我的研究让我知道我必须在某处使用@JvmSuppressWildcards,但我不确定在哪里。

Dagger provides a dependency based on class types in the graph, in your first code snippet of CoreModule it fails to provide dependencies into the appInfo(String, String) method because it cannot figure out which String type belongs where. Dagger 提供了基于图中 class 类型的依赖项,在您的 CoreModule 的第一个代码片段中,它无法为appInfo(String, String)方法提供依赖项,因为它无法确定哪个 String 类型属于哪里。 The fix is simple;修复很简单; annotate the dependencies that you have already provided with the @Named qualifier in the method that requires them within the module:在模块中需要它们的方法中使用@Named限定符注释您已经提供的依赖项:

@Module
class CoreModule {

    @Provides
    @Named("appName")
    fun appName() : String{
        return "Uburu Store"
    }

    @Provides
    @Named("appVersion")
    fun appVersion() : String {
        return "0.0.1"
    }

    @Provides
    @Named("appInfo")
    fun appInfo(@Named("appName") appName: String, @Named("appVersion") appVersion: String) : String {
        return "$appName Version:: $appVersion"
    }
}

Your second example works because there is only one String type to be provided from the graph.您的第二个示例有效,因为图中仅提供一种 String 类型。

For more information head over to the documentation and scroll to Qualifier section有关更多信息,请转到文档并滚动到 Qualifier 部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM