我创建了dagger2
Conponent
和Subcomponent
主要组成部分:
@Singleton
@Component(modules = {PresentersModule.class, RepositoriesModule.class, UtilsModule.class, AppModule.class, RoomModule.class})
public interface AppComponent {
SettingsComponent plusSettingsComponent(NetworkModule networkModule);
...
我的子组件:
@Upgradable
@Subcomponent(modules = {NetworkModule.class})
public interface SettingsComponent {
RestApiFactory getRestApiFactory();
}
我希望RestApiFactory
subcomponent
类似参数设置main component
@Singleton
@Provides
TransactionsRepository provideTransactionsRepository(RestApiFactory restApiFactory) {
return new TransactionsRepositoryImpl(restApiFactory);
}
但RestApiFactory
包含Subcomponent
现在我在Application类中创建了这个方法:
public SettingsComponent plusSettingsComponent() {
if (settingsComponent == null) {
settingsComponent = appComponent.plusSettingsComponent(new NetworkModule());
}
return settingsComponent;
}
并在存储库中调用它:
return MyApplication.me().plusSettingsComponent().getRestApiFactory()
.getTransactionsService()
.getTransactions(transactionRequest, page);
如果我尝试设置RestApiFactory
之类的参数 - 我会收到错误:
我想到这个方法:
@Singleton
@Provides
TransactionsRepository provideTransactionsRepository(RestApiFactory restApiFactory) {
return new TransactionsRepositoryImpl(restApiFactory);
}
并改变这一点:
private RestApiFactory restApiFactory;
@Inject
public TransactionsRepositoryImpl(RestApiFactory restApiFactory) {
this.restApiFactory = restApiFactory;
}
@Override
public Observable<List<TransactionItem>> loadTransactions(TransactionRequest transactionRequest, int page) {
return restApiFactory.getTransactionsService()
.getTransactions(transactionRequest, page);
}
错误
Error:(30, 27) error: retrofit2.Retrofit cannot be provided without an @Inject constructor or from an @Provides-annotated method.
完整错误
Error:(30, 27) error: retrofit2.Retrofit cannot be provided without an @Inject constructor or from an @Provides-annotated method.
retrofit2.Retrofit is injected at
my.network.api.RestApiFactory.<init>(retrofit)
my.network.api.RestApiFactory is injected at
my.dagger.RepositoriesModule.provideTransactionsRepository(restApiFactory)
my.repositories.TransactionsRepository is injected at
my.dagger.PresentersModule.provideTransactionsPresenter(…, transactionsRepository)
my.presenters.TransactionsPresenter is provided at
my.dagger.AppComponent.getTransactionsPresenter()
A binding with matching key exists in component: my.dagger.SettingsComponent
答案 0 :(得分:0)
查看错误消息的堆栈跟踪:
Error:(30, 27) error: retrofit2.Retrofit cannot be provided without an @Inject constructor or from an @Provides-annotated method.
retrofit2.Retrofit is injected at
my.network.api.RestApiFactory.<init>(retrofit)
my.network.api.RestApiFactory is injected at
my.dagger.RepositoriesModule.provideTransactionsRepository(restApiFactory)
my.repositories.TransactionsRepository is injected at
my.dagger.PresentersModule.provideTransactionsPresenter(…, transactionsRepository)
my.presenters.TransactionsPresenter is provided at
my.dagger.AppComponent.getTransactionsPresenter()
A binding with matching key exists in component: my.dagger.SettingsComponent
它表示您正在Retrofit
注入RestApiFactory
。
RestApiFactory
时,RepositoriesModule
会提供 TransactionsRepository
反过来,
TransactionsRepository
会在PresentersModule
中注入,您可以在getPresenter
的提供方法AppComponent
中提供该Retrofit
。
因此,您似乎正在尝试在超级组件(SettingsComponent
)中使用来自AppComponent
中绑定的子组件(SettingsComponent
)的绑定。这解释了Retrofit
错误消息中存在的&#34;绑定密钥 - {{1}}确实已绑定但未以正确方式绑定。
您不能使用超级组件中子组件的绑定 - 依赖关系向下冒泡而不是向上浮动。要在多个子组件中使用的依赖项应绑定在顶层并暴露给子组件。