如何在仍然使用 Dagger2 的同时解决循环依赖?

How to resolve a circular dependency while still using Dagger2?

我有两个 类、Foo<T>Bar,它们相互依赖,以及其他各种 类。我正在使用 Dagger-2 进行依赖注入,但如果我天真地添加循环依赖,Dagger 在运行时会发生堆栈溢出。什么是重构 类 来解决这个问题的好方法,同时仍然使用 Dagger 注入所有其他依赖项,并且对现有调用的重复和更改最少?

经过与同事的大量思考和讨论,我们最终做出了以下决定:

class Foo<T> extends FooWithoutDep<T> {
    @Inject Foo(Bar bar, OtherDep1 dep1, OtherDep2 dep2) {
        super(dep1, dep2);
        setBarDep(bar);
    }
}

class FooWithoutDep<T> {
    //Field declarations elided
    @Inject FooWithoutDep(OtherDep1 dep1, OtherDep2 dep2) {
        //Normal constructor stuff
    }
    void setBarDep(Bar bar) { this.bar = bar; }

    //The rest of the actual logic
}

class Bar {
    //Field declarations elided
    @Inject Bar(FooWithoutDep<Thing> foo, OtherDep3 dep3) {
        this.foo = foo;
        this.foo.setBarDep(this);
        this.dep3 = dep3;
    }

    //Code that uses Foo and the other dependencies
}

解释这一点——我们将 Foo 的实际逻辑移到了父级 class (FooWithoutDep) 中,它将循环依赖作为可设置的字段而不是构造函数参数。然后原始 class 只包含一个构造函数,该构造函数采用循环依赖并调用 setter。另一个 class,Bar,依赖父级 (FooWithoutDep),并显式调用 setter,传递自身 (this)。这使得对 class 的所有现有引用保持不变,同时仍然使用 Dagger 注入所有依赖项。

这似乎令人困惑,值得写在这里。

最简单的方法是在一侧使用 Lazy<T>

Lazy<Foo> foo;

@Inject
Bar(Lazy<Foo> foo) {
    this.foo = foo;
}

// use foo.get(); when needed

我就是这样解决的,没有父 classes.

Class 1:引擎。 (在组件界面中)

@Provides
public Engine myEngine(Context context) {
    return new Engine (context);
}

Class 2:零件。 Engine也需要Parts实例,但是创建延迟了

@Inject
public Parts(Context context, Engine engine) {
    this.context = context;
    this.engine= engine;
    engine.setParts(this);
}

可以实现循环依赖,但必须先启动一个 class。

同样,如果可能,重构代码以避免循环 DI。