我想让我的 Dagger 2 注入(inject)下面的类并且不打算使用 @Module
这样做。
所以我把 @Inejct constructor
如下
class InjectClass @Inject constructor(var txt: String = "Default")
它不起作用,因为它提示
e: [kapt] An exception occurred: java.lang.IllegalStateException: Found multiple
@Inject constructors: [InjectClass(java.lang.String), InjectClass()]
有没有办法让它在这里工作?
最佳答案
您可以编写辅助构造函数,而不是使用默认值:
class InjectClass @Inject constructor(var txt: String) {
constructor(): this("Default")
}
像这样,您确保默认构造函数没有用
@Inject
注释。 Dagger 知道如何创建 InjectClass
.可选地,我们可以这样做
class InjectClass(var txt: String) {
@Inject constructor(): this("Default")
}
关于kotlin - Kotlin 中的 Dagger 2 : Is there a way to do injection for class with default parameterized constructor without using Module?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53245340/