How model circular dependency in dagger2?

aQuu :

How can I model circular dependency using dagger2? Lets say we have only two classes. First injection is via constructor and second is via method as in example below:

class A{
    private B b;

    @Inject
    public A(B b)
    {
        this.b = b;
    }
}

class B{
    private A a;

    @Inject
    public B() { }

    @Inject
    public void injectA(A a)
    {
        this.a = a;
    }
}
Alex Filatov :

You can use lazy injection:

class B{
    private Lazy<A> a;

    @Inject
    public B(Lazy<A> a) {
       this.a = a;
    }
}

Alternatively you can inject Provider<A>, but be aware that provider returns new instance of A each time Provider::get is invoked (assuming default scope), while Lazy::get returns the same instance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to resolve a circular dependency while still using Dagger2?

How to avoid circular dependency with Dagger 2?

How to instantiate an instance of our dependency graph in Dagger2

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

How do you provide a GoogleApiClient dependency with Dagger2?

Dagger2 dependency - Gradle

How not to get a circular dependency?

How to solve the circular dependency

Dagger2 How to perform constructor injection which has dependency of a default constructor

Dependency Injection in Adapter in kotlin Dagger2

Dagger2 not resolving dependency all the way

Internal dependency injection using Dagger2

Android Dagger2 Dependency Injection

Dependency Injection - Dagger2 - Generics

How to solve Autofac circular dependency?

How to resolve circular dependency in Gradle

How to solve this circular dependency issue?

circular dependency, how to get rid

How to resolve circular dependency in DbContext

Circular dependency - how to resolve and avoid

How to resolve this circular dependency in EF

How to avoid circular dependency in this project?

How to refactor this to avoid a circular dependency?

How can inject Model to Presenter when create it wit dagger2?

How to set and get Singleton object of a model class using dagger2?

How to invoke Presenter Methods in Model file with Dagger2 and MVP pattern

Dagger 2: error: [ComponentProcessor:MiscError] circular dependency with generated code

Circular dependency with Angular 2 and SystemJS

Circular dependency injection angular 2