How to resolve a circular dependency while still using Dagger2?

后端 未结 3 1479
猫巷女王i
猫巷女王i 2021-02-12 15:29

I have two classes, Foo and Bar, which depend on each other, as well as various other classes. I am using Dagger-2 for dependency injection, b

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-12 16:19

    The easy way out is to use Lazy on one side.

    Lazy foo;
    
    @Inject
    Bar(Lazy foo) {
        this.foo = foo;
    }
    
    // use foo.get(); when needed
    

提交回复
热议问题