繁体   English   中英

Dagger 2 组件依赖

[英]Dagger 2 component dependencies

是否可以向模块中注入一些东西?

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

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

    OkHttpClient provideOkHttpClient();
}

@Module
public class OkHttpModule {

    @Provides
    OkHttpClient provideOkHttpClient() {

        return mHttpClient;
    }
}
@Component(modules = {GsonModule.class})
public interface GsonComponent extends AppComponent {

    Gson provideGson();
}

@Module
public class GsonModule {

    @Provides
    Gson provideGson() {

        return mGson;
    }
}

使用此模块/组件,我想创建第三个模块:

@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;
    }
}

但是没有注入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();

在我看来,注入模块并没有什么意义。 根据 Dagger1 模块的定义,所有模块都被声明为complete=false, library=true ,这意味着只要您包含不包含在您正在使用的组件中的模块,您就不需要指定任何真正的魔法,或者使您的组件依赖项以这样一种方式相互链接,即您可以注入的每个依赖项仅在一个组件中指定。

您将从构造函数参数中的其他模块获取依赖项。 您需要将模块includesincludes列表中,或者您需要在相同范围内的组件中指定模块。

正确的做法是这样的:

@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);
    }
}

然后就是

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ApplicationScope {}

甚至可能

@ApplicationScope
public class Something {
    private final VeentsMeAPI veentsMeApi;

    @Inject
    public Something(VeentsMeAAPI veentsMeApi) {
        this.veentsMeApi = veentsMeApi;
    }
}

然后

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

    void inject(SomeActivity someActivity);
}

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

AppComponent appComponent = DaggerAppComponent.create();

//use appComponent.inject(someActivity); with `void inject(Stuff someActivity);` methods defined in AppComponent

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM