2017-09-03 3 views
1

다른 Retrofit 인스턴스를 구성하는 두 개의 @Provides 메소드가있는 대거 모듈이 있습니다. 또한 각각 Retrofit 인스턴스 중 하나를 소비해야하는 두 가지 방법이 있습니다.동일한 유형의 두 인스턴스를 제공하십시오.

소비 기능 각각에서 Retrofit을 사용하고 싶다고 Dagger에게 어떻게 알릴 수 있습니까?

내 코드 :

@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 주석을 사용할 수 있습니다.

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

당신이 할 수있는 루게릭 병 o 자신 만의 특수 효과를 만들고 싶지 않은 경우 @Named을 사용하십시오. See the documentation here. 또한 이름 매개 변수를

사용이 코드 당신은 분사 주석이 사용이라는 주석을 얻을 수

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

을 사용할 수 있습니다

1

.

@Inject 
@Named("Google") 
Retrofit retrofit 

또한, 아동 referance에 대한 구성 요소를 추가 할 수 있습니다

@Named("Google") 
Retrofit providePlaceApiClient(); 
관련 문제