Dagger 2:当 parent 和 child 具有相同的注入签名时缺少提供者?
Dagger 2: Missing Provider when both parent and child have same inject singnature?
我正在使用 Dagger 2.24。当我编译下面的
fun main() {
val myClass = MyClass()
}
class MyClass {
@Inject
lateinit var stringMe: String
init {
DaggerMyComponent.create().subComponent().inject(this)
println(stringMe)
}
}
@Component
interface MyComponent {
fun subComponent(): MySubcomponent
fun inject(a: MyClass)
}
@Subcomponent(modules = [MeSubModule::class])
interface MySubcomponent {
fun inject(a: MyClass)
}
@Module
class MeSubModule {
@Provides
fun stringMe(): String = "Hi here"
}
它错误地指出
error: [Dagger/MissingBinding] java.lang.String cannot be provided without an @Inject constructor or an @Provides-annotated method.
public abstract interface MyComponent {
^
A binding with matching key exists in component: com.elyeproj.modular1bottombase.MySubcomponent
java.lang.String is injected at
com.elyeproj.modular1bottombase.MyClass.stringMe
com.elyeproj.modular1bottombase.MyClass is injected at
com.elyeproj.modular1bottombase.MyComponent.inject(com.elyeproj.modular1bottombase.MyClass)
然后我把下面简单的一行注释掉,全部编译正常
fun main() {
val myClass = MyClass()
}
class MyClass {
@Inject
lateinit var stringMe: String
init {
DaggerMyComponent.create().subComponent().inject(this)
println(stringMe)
}
}
@Component
interface MyComponent {
fun subComponent(): MySubcomponent
// fun inject(a: MyClass) // Comment this out.
}
@Subcomponent(modules = [MeSubModule::class])
interface MySubcomponent {
fun inject(a: MyClass)
}
@Module
class MeSubModule {
@Provides
fun stringMe(): String = "Hi here"
}
怀疑这是 Dagger 2 的错误,但请写在这里以防我遗漏任何内容?
显然,只要我们在 MyComponent
中有 inject(a: MyClass)
,在编译时,它会尝试验证 MyComponent
是否能够满足所有 @Inject
MyClass
需要的依赖项。
@Component
interface MyComponent {
fun subComponent(): MySubcomponent
fun inject(a: MyClass) // Comment this out.
}
在我上面的例子中,MyClass
需要 String
,但是 MyComponent
本身没有能力提供 String
。所以它失败了。当前注释编译会进行检查,并且实际上是否 已连接 并不重要。
我正在使用 Dagger 2.24。当我编译下面的
fun main() {
val myClass = MyClass()
}
class MyClass {
@Inject
lateinit var stringMe: String
init {
DaggerMyComponent.create().subComponent().inject(this)
println(stringMe)
}
}
@Component
interface MyComponent {
fun subComponent(): MySubcomponent
fun inject(a: MyClass)
}
@Subcomponent(modules = [MeSubModule::class])
interface MySubcomponent {
fun inject(a: MyClass)
}
@Module
class MeSubModule {
@Provides
fun stringMe(): String = "Hi here"
}
它错误地指出
error: [Dagger/MissingBinding] java.lang.String cannot be provided without an @Inject constructor or an @Provides-annotated method.
public abstract interface MyComponent {
^
A binding with matching key exists in component: com.elyeproj.modular1bottombase.MySubcomponent
java.lang.String is injected at
com.elyeproj.modular1bottombase.MyClass.stringMe
com.elyeproj.modular1bottombase.MyClass is injected at
com.elyeproj.modular1bottombase.MyComponent.inject(com.elyeproj.modular1bottombase.MyClass)
然后我把下面简单的一行注释掉,全部编译正常
fun main() {
val myClass = MyClass()
}
class MyClass {
@Inject
lateinit var stringMe: String
init {
DaggerMyComponent.create().subComponent().inject(this)
println(stringMe)
}
}
@Component
interface MyComponent {
fun subComponent(): MySubcomponent
// fun inject(a: MyClass) // Comment this out.
}
@Subcomponent(modules = [MeSubModule::class])
interface MySubcomponent {
fun inject(a: MyClass)
}
@Module
class MeSubModule {
@Provides
fun stringMe(): String = "Hi here"
}
怀疑这是 Dagger 2 的错误,但请写在这里以防我遗漏任何内容?
显然,只要我们在 MyComponent
中有 inject(a: MyClass)
,在编译时,它会尝试验证 MyComponent
是否能够满足所有 @Inject
MyClass
需要的依赖项。
@Component
interface MyComponent {
fun subComponent(): MySubcomponent
fun inject(a: MyClass) // Comment this out.
}
在我上面的例子中,MyClass
需要 String
,但是 MyComponent
本身没有能力提供 String
。所以它失败了。当前注释编译会进行检查,并且实际上是否 已连接 并不重要。