How to resolve a circular dependency while still using Dagger2?

Jesse W at Z - Given up on SE :

I have two classes, Foo<T> and Bar, which depend on each other, as well as various other classes. I am using Dagger-2 for dependency injection, but if I naively add the circular dependency, Dagger hits a stack overflow at runtime. What's a good way to refactor the classes to fix this, while still using Dagger to inject all the other dependencies, and with minimal duplication and changes to existing calls?

EpicPandaForce :

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

Lazy<Foo> foo;

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

// use foo.get(); when needed

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unable to inject dependency in fragment using dagger2 in Java Android

How to resolve circular dependency in Gradle

How model circular dependency in dagger2?

How to resolve circular dependency for Spring context?

Resolve circular dependency in gradle

How to resolve maven problems when using dependency injection to solve circular dependency?

How to test with Dagger2 Dependency Injection & Robolectric in Android?

How do you provide a GoogleApiClient dependency with Dagger2?

How to resolve composer package circular dependency of external package?

How to resolve circular dependency in DbContext

Resolve circular dependency in Swift

Error while using Dagger2, Room and Databinding

Circular dependency between the following tasks while using firebase Crashlytics

How do I resolve this Angular 7 "Circular dependency detected" warning

Dagger2 dependency Cycle by Using @Binds and @Inject fields

Dao is null while using Dagger2

How to resolve this circular dependency in EF

How to avoid circular dependency while adding references to windows runtimecomponent?

Internal dependency injection using Dagger2

Circular dependency - how to resolve and avoid

How to instantiate an instance of our dependency graph in Dagger2

How to avoid circular dependency with Dagger 2?

Is there a way to resolve this template circular dependency

Is it still necessary to check an object for nullity if it is initialized using Dagger2?

How to resolve Loop carried dependency preventing loop vectorization while using -fast in PGI compiler in OpenACC

How to resolve a circular dependency in nest js?

How to resolve dependency conflict while installing Bootstrap using Angular CLI command?

How to resolve circular dependency when working with IAM roles

How to resolve circular dependency in class constructors?