2017-09-03 96 views
1

我有一个Dagger模块,它有两个构造不同Retrofit实例的@Provides方法。我也有两种方法需要使用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

您可以使用@Qualifier注解区分两者。

首先创建一个新的注释类型(当然它自己的Java文件):

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

} 

然后注释相关@Provides方法:

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

然后告诉改造使用这一个在其他@Provides

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

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

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(); 
相关问题