匕首模块
@Module
public class NetModule {
@Provides
@Singleton
Gson provideGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
return gsonBuilder.create();
}
@Provides
@Singleton
OkHttpClient provideOkHttpClient() {
OkHttpClient client = new OkHttpClient();
return client;
}
@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(BuildConfig.SERVER_BASE_URL)
.client(okHttpClient)
.build();
return retrofit;
}
}
@Module
public class AppModule{
private Application application;
public AppModule(Application application){
this.application = application;
}
@Provides
@Singleton
Application provideApplication() {
return application;
}
}
@Module
public class ImageModule {
@Provides
@Singleton
Picasso providePicasso(Application application, OkHttpClient client) {
Picasso.Builder builder = new Picasso.Builder(application);
builder.downloader(new OkHttp3Downloader(client));
return builder.build();
}
}
这些是组成部分
@Singleton
@Component(modules={NetModule.class})
public interface NetComponent {
void inject(MyFragment fragment);
}
@Singleton
@Component(modules={AppModule.class, ImageModule.class}, dependencies = {NetModule.class})
public interface ImageComponent {
void inject(MyFragment fragment);
}
这就是我注册组件的方式
public class MyApp extends Application{
@Override
public void onCreate() {
netComponent = DaggerNetComponent.builder()
.netModule(new NetModule())
.build();
imageComponent = DaggerImageComponent.builder()
.appModule(new appModule(this))
.imageModule(new ImageModule())
.build();
}
}
并在片段中
public class MyFragment extends Fragment {
@Inject
Retrofit retrofit;
@Inject
Picasso picasso;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
((MyApp)getActivity().getApplication()).getNetComponent().inject(this);
((MyApp)getActivity().getApplication()).getImageComponent().inject(this);
....
}
}
我收到如下编译错误
错误:(25,10)错误:无法提供com.squareup.picasso.Picasso
没有@Inject构造函数或@ Provides-或
@Produces注释的方法。
在匕首2中实现此目标的最佳方法是什么?
最佳答案
当组件A依赖于类型B时,您是说B上的每个零精简吸气剂将在A的图中可用。这意味着您不需要在NetComponent上单独调用inject
,但是需要在NetComponent的定义中公开OkHttpClient。
这意味着NetComponent将如下所示:
@Singleton
@Component(modules={NetModule.class})
public interface NetComponent {
/** Exposes OkHttpClient. Used by components depending on NetComponent. */
OkHttpClient getOkHttpClient();
}
您的
onCreateView
可能看起来像这样:@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
((MyApp)getActivity().getApplication()).getImageComponent().inject(this);
}
在内部,Dagger通过两个单独的步骤生成DaggerNetComponent和DaggerImageComponent,但是ImageComponent调用了新的
getOkHttpClient method
。这也意味着DaggerImageComponent可以接受NetComponent的任何实现,而不仅仅是Dagger生成的一个。相关资源:
What is the purpose of the non-inject methods in Components in Dagger 2?(请参阅David Rawson的答案)
Dagger 2 subcomponents vs component dependencies
component dependencies上的Dagger文档及其指向provision methods的链接