Dagger 2组件依赖项

Ral*_*ann 11 java android dependency-injection dagger-2

是否可以将某些东西注入模块中?

我有2个基本模块/组件:

@Component(modules = {OkHttpModule.class})
public interface OkHttpComponent extends AppComponent {

    OkHttpClient provideOkHttpClient();
}

@Module
public class OkHttpModule {

    @Provides
    OkHttpClient provideOkHttpClient() {

        return mHttpClient;
    }
}
Run Code Online (Sandbox Code Playgroud)
@Component(modules = {GsonModule.class})
public interface GsonComponent extends AppComponent {

    Gson provideGson();
}

@Module
public class GsonModule {

    @Provides
    Gson provideGson() {

        return mGson;
    }
}
Run Code Online (Sandbox Code Playgroud)

有了这个模块/组件,我想创建第3个模块:

@Component(dependencies = {OkHttpComponent.class, GsonComponent.class}, 
           modules = {RetrofitModule.class})
public interface RetrofitComponent extends AppComponent {

    API provideAPI();
}

@Module
public class RetrofitModule {

    @Inject OkHttpClient mHttpClient;
    @Inject Gson         mGson;

    @Provides
    VeentsMeAPI provideVeentsMeAPI() {

        return mVeentsMeAPI;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是mHttpClientmGson没有注入.是否可以将某些东西注入模块中?如果是的话怎么样?

我创建这样的组件:

okHttpComponent = DaggerOkHttpComponent.builder()
        .okHttpModule(new OkHttpModule(this))
        .build();

gsonComponent = DaggerGsonComponent.builder()
        .gsonModule(new GsonModule())
        .build();

retrofitComponent = DaggerRetrofitComponent.builder()
        .retrofitModule(new RetrofitModule())
        .okHttpComponent(okHttpComponent)
        .gsonComponent(gsonComponent)
        .build();
Run Code Online (Sandbox Code Playgroud)

Epi*_*rce 22

在我看来,注入模块并不是真的有意义.根据模块complete=false, library=true的Dagger1定义声明所有模块,这意味着只要包含未使用的组件中包含的模块,或者使组件依赖关系链接到每个模块,就不需要指定任何真正的魔术.另外,您可以注入的每个依赖项仅在一个组件中指定.

您将从构造函数参数中的其他模块获取依赖项.您需要在includes列表中包含该模块,或者您需要在组件中指定的模块位于同一范围内.

正确的方法是:

@Module
public class OkHttpModule {
    @Provides
    @ApplicationScope
    public OkHttpClient okHttpClient() {
        return new OkHttpClient();
    }
}

@Module
public class GsonModule {
    @Provides
    @ApplicationScope 
    public Gson gson() {
        return new Gson();
    }
}

@Module(includes={GsonModule.class, OkHttpModule.class}) //look closely, this is the magic
public class RetrofitModule {
    @Provides
    @ApplicationScope
    public VeentsMeAPI veentsMeAPI(Gson gson, OkHttpClient okHttpClient) { //dependencies!
         return RestAdapter.Builder()
            .setClient(new Client(okHttpClient))
            .setConverter(new GsonConverter(gson))
            .create(VeentsMeAPI.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后是

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ApplicationScope {}
Run Code Online (Sandbox Code Playgroud)

然后

@ApplicationScope
@Component(modules={RetrofitModule.class}) //contains all other modules/dependencies
public interface AppComponent {
    OkHttpClient okHttpClient();
    Gson gson();
    VeentsMeAPI veentsMeAPI();

    void inject(Something something);
}
Run Code Online (Sandbox Code Playgroud)

然后你会得到生成的东西,你必须像这样一起构建它

AppComponent appComponent = DaggerAppComponent.create();

//use appComponent.inject(stuff); with `void inject(Stuff stuff);` methods defined in AppComponent
Run Code Online (Sandbox Code Playgroud)

组件依赖项与子组件相同,因此它们用于子组件,而不是用于从同一范围继承依赖项.相同范围的依赖关系应该在同一个组件中,只是不同的模块.

  • "AppComponent"只是右侧范围的空接口,请参阅http://stackoverflow.com/a/30425419/1016472这个`RetrofitComponent retrofitComponent = DaggerRetrofitModule.builder().okHttpComponent(okHttpComponent).gsonComponent(gsonComponent).retrofitModule (retrofitModule).build();`就是我做的,但我希望有更好的方法. (2认同)