是否可以在模块中注入一些东西?
我有 2 个基本模块/组件:
@Component(modules = {OkHttpModule.class})
public interface OkHttpComponent extends AppComponent {
OkHttpClient provideOkHttpClient();
}
@Module
public class OkHttpModule {
@Provides
OkHttpClient provideOkHttpClient() {
return mHttpClient;
}
}
@Component(modules = {GsonModule.class})
public interface GsonComponent extends AppComponent {
Gson provideGson();
}
@Module
public class GsonModule {
@Provides
Gson provideGson() {
return mGson;
}
}
有了这个模块/组件,我想创建第三个模块:
@Component(dependencies = {OkHttpComponent.class, GsonComponent.class},
modules = {RetrofitModule.class})
public interface RetrofitComponent extends AppComponent {
API provideAPI();
}
@Module
public class RetrofitModule {
@Inject OkHttpClient mHttpClient;
@Inject Gson mGson;
@Provides
VeentsMeAPI provideVeentsMeAPI() {
return mVeentsMeAPI;
}
}
但是mHttpClient
并mGson
没有注入。是否可以在模块中注入一些东西?如果是的话怎么办?
我创建这样的组件:
okHttpComponent = DaggerOkHttpComponent.builder()
.okHttpModule(new OkHttpModule(this))
.build();
gsonComponent = DaggerGsonComponent.builder()
.gsonModule(new GsonModule())
.build();
retrofitComponent = DaggerRetrofitComponent.builder()
.retrofitModule(new RetrofitModule())
.okHttpComponent(okHttpComponent)
.gsonComponent(gsonComponent)
.build();