Dagger modules with constructor arguments?

In Guice, I took complete control over when the Modules were created, and used some Modules with the constructor arguments that I installed.

However, in the dagger, the method of referencing other modules via @Module includes an annotation and does not provide me with the same method of creating modules for installation.

Is it possible to create a normal ObjectGraph from several modules that have constructor arguments? Especially one that will work with a compiler dagger and not run in a circular graph?

+5
source share
2 answers

, , . , , :

@Module
public class ContextModule {

    private final Context mContext;

    public ContextModule(Context context) {
        mContext = context;
    }

    @Provides
    public Context provideContext() {
        return mContext;
    }

}

, , , .

:

@Module(entryPoints = { MyFragment.class }, includes = { ContextModule.class })
public class ServicesModule {

    @Provides
    public LocationManager provideLocationManager(Context context) {

        return (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }

    @Provides
    public Geocoder provideGeocoder(Context context) {
        return new Geocoder(context);
    }
}

, , , .

+3

ObjectGraph.create() (Varargs), :

ObjectGraph objectGraph = ObjectGraph.create(new ProductionModule(context), new OverridingTestModule());

Dagger InjectionTest.java(. "moduleOverrides" ): https://github.com/square/dagger/blob/master/core/src/test/java/dagger/InjectionTest.java

+3

All Articles