如果没有@Inject构造函数或@ Provide-annotated方法,则无法提供retrofit2.Retrofit

时间:2018-01-11 14:26:26

标签: android dagger-2

我创建了dagger2 ConponentSubcomponent

主要组成部分:

@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

1 个答案:

答案 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}}确实已绑定但未以正确方式绑定。

您不能使用超级组件中子组件的绑定 - 依赖关系向下冒泡而不是向上浮动。要在多个子组件中使用的依赖项应绑定在顶层并暴露给子组件。