Dagger 2注入两个改造对象 [英] Dagger 2 injecting two retrofit objects
问题描述
我在使用MVP时将Dagger 2与retrofit2库一起使用。一切顺利,直到我尝试集成另一个服务(基本上我试图将另一个改造对象初始化到另一个服务)。我遵循了这个 answer 的方法,但是没有成功。
I'm using Dagger 2 with retrofit2 library while using MVP. Everything went well till I tried to integrate another service (basically I tried to initialize another retrofit object to another service). I followed this answer but without any success.
每次出现错误时,我的每个片段和应用程序类似乎都无法识别组件类。
Every time I'm getting an errors that each of my fragments and application classes don't seem to recognize the component classes.
错误:找不到符号类DaggerApplicationComponent
错误:找不到符号类DaggerEpisodeComponent
error: cannot find symbol class DaggerApplicationComponent error: cannot find symbol class DaggerEpisodeComponent
代码
ApplicationComponent
@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
Retrofit exposeStreamingRetrofit();
Retrofit exposeRetrofit();
Context exposeContext();
AppPreferenceHelper exposePrefs();
}
应用程序模块
@Module
public class ApplicationModule
{
private String mBaseUrl;
private Context mContext;
private AppPreferenceHelper mPrefsHelper;
public ApplicationModule(Context context,String baseUrl)
{
mContext = context;
mBaseUrl = baseUrl;
mPrefsHelper = new AppPreferenceHelper(context, Consts.PREF_NAME);
}
@Singleton
@Provides
GsonConverterFactory provideGsonConverterFactory()
{
GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create();
return gsonConverterFactory;
}
@Singleton
@Provides
@Named("ok-1")
OkHttpClient provideOkHttpClient()
{
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
return new OkHttpClient().newBuilder()
.connectTimeout(500, TimeUnit.MILLISECONDS)
.readTimeout(500,TimeUnit.MILLISECONDS)
.addInterceptor(logging)
.build();
}
@Singleton
@Provides
RxJava2CallAdapterFactory provideRxJava2CallAdapterFactory()
{
return RxJava2CallAdapterFactory.create();
}
@Provides
@Singleton
Retrofit provideRetrofit(@Named("ok-1") OkHttpClient client, GsonConverterFactory convectorFactory, RxJava2CallAdapterFactory adapterFactory)
{
return new Retrofit.Builder()
.baseUrl(mBaseUrl)
.addConverterFactory(convectorFactory)
.addCallAdapterFactory(adapterFactory)
.client(client)
.build();
}
@Provides
@Singleton
Retrofit provideStreamingRetrofit(@Named("ok-1") OkHttpClient client, GsonConverterFactory convectorFactory, RxJava2CallAdapterFactory adapterFactory) {
return new Retrofit.Builder()
.baseUrl(Consts.STREAMING_BASE_PATH)
.addConverterFactory(convectorFactory)
.addCallAdapterFactory(adapterFactory)
.client(client)
.build();
}
@Singleton
@Provides
Context provideContext()
{
return mContext;
}
@Singleton
@Provides
AppPreferenceHelper provideAppPreferenceHelper(){
return mPrefsHelper;
}
}
StreamingService >
public interface StreamingService
{
@GET("search")
Observable<StreamingItems> getStreamingItems(@Query("keyword") String query);
}
流模块
@Module
Streaming Module @Module
public class StreamingModule
{
private StreamingView mView;
public StreamingModule(StreamingView view)
{
mView = view;
}
@PerFragment
@Provides
StreamingService provideStreamingService(Retrofit retrofit)
{
return retrofit.create(StreamingService.class);
}
@PerFragment
@Provides
StreamingView provideView()
{
return mView;
}
public void disposeView()
{
mView = null;
}
}
流媒体组件
@PerFragment
@Component(modules = StreamingModule.class, dependencies = ApplicationComponent.class)
public interface StreamingComponent {
void inject(StreamingFragment streamingFragment);
}
Streaming Presenter
public class StreamingPresenter extends BasePresenter<StreamingView>
{
private long mMaxPagesOfTopSeries;
@Inject
protected StreamingService mApiService;
@Inject
protected Mapper mTopSeriesMapper;
@Inject
protected AppPreferenceHelper mPrefsHelper;
@Inject
public StreamingPresenter()
{
mMaxPagesOfTopSeries = 1;
}
}
问题可能与在组件应用程序类中公开Retrofit的另一个实例,但我不确定。
The problem might be connected to the exposing another instance of Retrofit in the component application class, but I'm not sure.
更新1
EpisodeModule
@PerFragment
@Component (modules = EpisodeModule.class, dependencies = ApplicationComponent.class)
public interface EpisodeComponent
{
void inject(EpisodeFragment episodeFragment);
}
创建流类(服务,演示者,模块,组件)后,没有在其他类中进行任何更改,所以我认为问题出在应用程序模块/组件或流式类中,但是我不确定,因为我可能会更改其他片段的改造对象,因为我添加了流式改造的新实例。
After I created the streaming classes (service, presenter, module, component) I didn't change anything in other classes so I think the problem is somewhere in Application module/component or streaming classes but I'm not sure because I might change the retrofit object for the other fragments because I added a new instance of retrofit for streaming.
推荐答案
正确的问题是应用程序模块中第二个Retrofit公开实例。解决方案是使用匕首限定符。只需将适当的代码块替换为:
You are right the problem is the second exposed instance of Retrofit in application module.The solution is to use dagger qualifiers. Just replace appropriate code blocks with:
在中使用限定符 @Named( streaming)
定义改造提供程序>应用程序模块
Define retrofit provider with qualifier @Named("streaming")
in Application Module
@Provides
@Singleton
@Named("streaming")
Retrofit provideStreamingRetrofit(@Named("ok-1") OkHttpClient client, GsonConverterFactory convectorFactory, RxJava2CallAdapterFactory adapterFactory) {
return new Retrofit.Builder()
.baseUrl(Consts.STREAMING_BASE_PATH)
.addConverterFactory(convectorFactory)
.addCallAdapterFactory(adapterFactory)
.client(client)
.build();
}
不要忘记在中公开具有完全相同的修饰符的改造实例应用程序组件
@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
@Named("streaming") Retrofit exposeStreamingRetrofit();
Retrofit exposeRetrofit();
Context exposeContext();
AppPreferenceHelper exposePrefs();
}
无论何时需要流服务改造实例-都不需要忘记设置预选赛。 流模块中的示例
Whenever you need the streaming service retrofit instance - don't forget to set qualifier. Example in Streaming Module
@PerFragment
@Provides
StreamingService provideStreamingService(@Named("streaming") Retrofit retrofit) {
return retrofit.create(StreamingService.class);
}
这篇关于Dagger 2注入两个改造对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!