错误:[Dagger/DepertencyCycle]发现了一个用Dagger2的依赖周期
我试图在组件构建器中使用@bindsinstance,但不能将依赖性注入一个
是组件的模块:
@Component(modules = [MainModule::class])
interface MainComponent {
fun inject(activity: MainActivity)
@Component.Builder
interface Builder {
@BindsInstance
fun mainDep(dep: MainTest): Builder
fun build(): MainComponent
}
}
那就是模块:
@Module
class MainModule {
@Provides
fun provideMainTest(mainTest: MainTest) = mainTest
}
那是要注射的类:
interface MainTest {
fun mainTest()
}
class MainTestImpl: MainTest {
override fun mainTest() {
println("main test")
}
}
但是当我尝试将其注入MainActivity时,我会得到一个编译器错误
error: [Dagger/DependencyCycle] Found a dependency cycle:
public abstract interface MainComponent {
^
com.example.testdagger.di.MainTest is injected at
com.example.testdagger.di.MainModule.provideMainTest(mainTest)
com.example.testdagger.di.MainTest is injected at
com.example.testdagger.di.MainModule.provideMainTest(mainTest)
I am trying to use @BindsInstance in a Component Builder, but can't inject dependency into a module
That is the component:
@Component(modules = [MainModule::class])
interface MainComponent {
fun inject(activity: MainActivity)
@Component.Builder
interface Builder {
@BindsInstance
fun mainDep(dep: MainTest): Builder
fun build(): MainComponent
}
}
That is the module:
@Module
class MainModule {
@Provides
fun provideMainTest(mainTest: MainTest) = mainTest
}
That is the class to inject:
interface MainTest {
fun mainTest()
}
class MainTestImpl: MainTest {
override fun mainTest() {
println("main test")
}
}
But when I try to inject it into MainActivity I get a compiler error
error: [Dagger/DependencyCycle] Found a dependency cycle:
public abstract interface MainComponent {
^
com.example.testdagger.di.MainTest is injected at
com.example.testdagger.di.MainModule.provideMainTest(mainTest)
com.example.testdagger.di.MainTest is injected at
com.example.testdagger.di.MainModule.provideMainTest(mainTest)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否尝试说出这样的事情:
而且:
我知道这不是您想要实现的目标,但我想检查结果。另外,请提供如何创建组件以及如何注入活动。
Can you try and say what will happen like this:
and also:
I know that it is not exactly what you want to achieve, but I want to check the result. Also, please provide how you create the component and how do you inject the Activity.