gpt4 book ai didi

java - Dagger 2 组件依赖性不起作用

转载 作者:行者123 更新时间:2023-11-29 04:33:36 25 4
gpt4 key购买 nike

Dagger 模块

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

}

}

在 fragment 中

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);
....

}


}

编译错误如下

Error:(25, 10) error: com.squareup.picasso.Picasso cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.

在 Dagger 2 中实现此目标的最佳方法是什么?

最佳答案

当组件 A 依赖于类型 B 时,您是说 B 上的每个零参数 getter 都将在 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 方法。这也意味着 DaggerImageComponent 可以接受 NetComponent 的任何实现,而不仅仅是 Dagger 生成的实现。

相关资源:

关于java - Dagger 2 组件依赖性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42708919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com