提供两个相同类型的不同实例

时间:2017-09-03 13:56:11

标签: java android dagger-2 dagger

我有一个带有两个@Provides方法的Dagger模块,用于构造不同的Retrofit个实例。我还有两个方法,每个方法都需要使用Retrofit个实例之一。

如何告诉Dagger我想在每个消费函数中使用哪个Retrofit

我的代码:

@Provides
@Singleton
public OkHttpClient provideOkHttpClient(){
    final OkHttpClient.Builder builder = new OkHttpClient.Builder();

    if (BuildConfig.DEBUG) {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(logging);
    }

    builder.connectTimeout(60 * 1000, TimeUnit.MILLISECONDS)
            .readTimeout(60 * 1000, TimeUnit.MILLISECONDS);

    return builder.build();
}

@Provides
@Singleton
public Retrofit provideRestAdapter1(Application application, OkHttpClient okHttpClient) {
    Retrofit.Builder builder = new Retrofit.Builder();
    builder.client(okHttpClient)
            .baseUrl(application.getString(R.string.Endpoint1))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create());
    return builder.build();
}

@Provides
@Singleton
public Retrofit provideRestAdapter2(Application application, OkHttpClient okHttpClient) {
    Retrofit.Builder builder = new Retrofit.Builder();
    builder.client(okHttpClient)
            .baseUrl(application.getString(R.string.Endpoint2))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create());
    return builder.build();
}

@Provides
@Singleton
public GithubApiService provideGithubApiService(Retrofit restAdapter) {
    return restAdapter.create(GithubApiService.class);
}

@Provides
@Singleton
public GithubApiService2 provideGithubApiService(Retrofit restAdapter) {
    return restAdapter.create(GithubApiService2.class);
}

}

2 个答案:

答案 0 :(得分:2)

您可以使用@Qualifier注释来区分这两者。

首先创建一个新的注释类型(当然是在自己的java文件中):

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface EndPoint1 {

}

然后注释相关的@Provides方法:

@Provides
@Singleton
@EndPoint1
public Retrofit provideRestAdapter1(Application application, OkHttpClient okHttpClient) {
    ...
}

然后告诉Retrofit在另一个@Provides中使用这个:

@Provides
@Singleton
public GithubApiService provideGithubApiService(@EndPoint1 Retrofit restAdapter) {
    return restAdapter.create(GithubApiService.class);
}

如果您不想创建自己的注释,也可以使用@NamedSee the documentation here

答案 1 :(得分:1)

您还可以使用名称参数

使用此代码

@Provides
@Singleton
@Named("Google")
Retrofit providePlaceApiClient(OkHttpClient client, Gson gson) {
    return new Retrofit.Builder()
            .baseUrl(BaseApiConfig.getPlaceApiUrl())
            .client(client)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();
}

@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient client, Gson gson) {
    return new Retrofit.Builder()
            .baseUrl(BaseApiConfig.getBaseUrl())
            .client(client)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();
}

您可以使用带注入注释的命名注释来获取此内容。

@Inject
@Named("Google")
Retrofit retrofit

此外,您可以添加组件以进行子女抚养

@Named("Google")
Retrofit providePlaceApiClient();