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
This is how I resolved it, without parent classes.
Class 1: Engine. (in component interface)
@Provides
public Engine myEngine(Context context) {
return new Engine (context);
}
Class 2: Parts. Engine also needs Parts instance but the creation is delayed.
@Inject
public Parts(Context context, Engine engine) {
this.context = context;
this.engine= engine;
engine.setParts(this);
}
Circular dependency can be achieved but one class must be initiated first before the other.
Again, if possible, refactor code to avoid circular DI.