Java – dagger 2

Using dagger 2, I tried to inject singleton objects at multiple locations within a single range However, it seems that my solution creates a new instance every time

In this test project, I have a mainactivity to initialize the daggermodule Daggermodule provides object @ r_ 53_ 2419 @ and cat, @ r_ 53_ 2419 @ take cat as the parameter I also hold cats in my main activities Finally, I check the references of two cat variables (in @ r_53_2419 @ and mainactivity respectively), but they are not the same instances

If I called provideCat () two times in MainActivity, I provided the same instance.

Main activities:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DaggerModule daggerModule = new DaggerModule();
        DaggerComponent daggerComponent = Dagger_DaggerComponent.builder()
                .daggerModule(daggerModule).build();

        // Same Cat instance returned.
        Cat cat1 = daggerComponent.provideCat();
        Cat cat2 = daggerComponent.provideCat();
        Log.d("=== cat1: ",cat1.toString());
        Log.d("=== cat2: ",cat2.toString());

        // Different Cat instance returned. Why?
        @R_53_2419@ @R_53_2419@ = daggerComponent.provide@R_53_2419@();
        Log.d("=== @R_53_2419@ cat: ",@R_53_2419@.getCat().toString());
    }
}
@Module
public class DaggerModule {

    @Provides
    @Singleton
    public Cat provideCat() {
        return new Cat();
    }

    @Provides
    @Singleton
    public @R_53_2419@ provide@R_53_2419@() {
        return new @R_53_2419@(provideCat());
    }

}
@Singleton
@Component(modules = { DaggerModule.class })
public interface DaggerComponent {

    Cat provideCat();

    @R_53_2419@ provide@R_53_2419@();

}
public class Cat {

    @Inject
    public Cat() {
    }

}
public class @R_53_2419@ {

    private Cat mCat;

    @Inject
    public @R_53_2419@(Cat cat) {
        mCat = cat;
    }

    public Cat getCat() {
        return mCat;
    }

}

Thank you in advance!

Edit: if provide@R_53_2419 @Receives a cat parameter and uses it to create a @ R_ 53_ 2419 @, not directly from provide@R_53_2419 Call provideCat in @, it will work.

// Doesn't work,new Cat instance created.
    @Provides
    @Singleton
    public @R_53_2419@ provide@R_53_2419@() {
        return new @R_53_2419@(provideCat());
    }

VS

// Works,same singleton injected.
    @Provides
    @Singleton
    public @R_53_2419@ provide@R_53_2419@(Cat cat) {
        return new @R_53_2419@(cat);
    }

Calling provideCat in MainActivity and in DaggerModule provide@R_53_2419 @What's the difference between calling in? Maybe the dagger compiler doesn't deal with the daggermodule as it does with external classes, and if I call providecat there, the comments won't be applied?

Solution

I want to start from offer@R_53_2419 The reason for calling provideCat is that I misunderstand some of my Component interfaces. I misunderstood that the component interface is not actually implemented by the module, so the parameters of the module method do not have to be declared in the corresponding method of the component If so, then doing so will force me to work in mainactivity provide@R_53_2419 @I want to avoid creating a cat instance in the method call (so directly in the provide@R_53_2419 @ call provideCat in the method. In fact, declaring parameters in the component method even prevents the dagger compiler from compiling

However, since the Component method does not refer to parameters, the solution only takes the instance as a parameter in the Module method needed to inject (instead of calling the corresponding method from Module), instead of calling the Component's parameter free method from MainActivity as follows:

Main activities:

Cat cat = daggerComponent.provideCat();
@R_53_2419@ @R_53_2419@ = daggerComponent.provide@R_53_2419@();

spare parts:

Cat provideCat();
@R_53_2419@ provide@R_53_2419@(); <- no arguments

modular:

@Module
public class DaggerModule {

    @Provides
    @Singleton
    public Cat provideCat() {
        return new Cat();
    }

    @Provides
    @Singleton
    public @R_53_2419@ provide@R_53_2419@(Cat cat) { <- arguments
        return new @R_53_2419@(cat);
    }

}

Mainactivity and @ r_ 53_ Cat singleton instances of 2419 @ are now the same. I don't need to declare them from mainactivity, but dagger handles all this success! I still don't know why the provider method works differently when called by an external class than the module itself

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>