如果没有@Annex-annotated方法,则无法提供对象

时间:2017-02-27 06:53:19

标签: android dependency-injection retrofit2 dagger-2 inject

我已经开始设置Dagger 2并面临一个奇怪的问题,看起来像是一个bug。

我在父组件中有1个主要组件和1个子组件。

我用:

compile "com.google.dagger:dagger:2.4"
apt "com.google.dagger:dagger-compiler:2.4"
annotationProcessor "com.google.dagger:dagger-compiler:2.4"

应用程序组件很简单。它只是'加'子组件:

@Singleton @Component(modules = { AristocratsAppModule.class })
public interface AristocratsAppComponent {
  StreamComponent plus(StreamModule module);
}

应用程序模块也很基础。它提供了应用程序级别的依赖性:

@Module public class AristocratsAppModule {
  private static AristocratsApp app;

  public AristocratsAppModule(AristocratsApp app) {
    AristocratsAppModule.app = app;
  }

  @Provides @Singleton AristocratsApp providesApplication() {
    return app;
  }

  @Provides @Singleton GsonXml providesGsonXml() {
    return ...
  }

  @Provides @Singleton @Named("aristocrats") Retrofit providesAristocratsRetrofit() {
    return ...
  }

  @Provides @Singleton @Named("last_fm") Retrofit providesLastFmRetrofit() {
    return ...
  }
}

子组件有两个StreamServices(Retrofit服务)和简单注入:

@PerController @Subcomponent(modules = { StreamModule.class }) public interface StreamComponent {
  void inject(StreamsController streamsController);

  AristocratsStreamService providesAristocratsStreamService();

  LastFmStreamService providesLastFmStreamService();
}

子组件模块提供演示者注入和两种不同的Retrofit服务注入:

@Module public class StreamModule {
  private StreamsController mView;

  public StreamModule(StreamsController view) {
    mView = view;
  }

  @Provides @PerController StreamMvp.Presenter providesPresenter(StreamCase streamCase,
      StreamModelMapper streamMapper) {
    return new StreamPresenter(mView, streamCase, streamMapper);
  }

  @Provides @PerController AristocratsStreamService providesAristocratsStreamService(
      @Named("aristocrats") Retrofit retrofit) {
    return retrofit.create(AristocratsStreamService.class);
  }

  @Provides @PerController LastFmStreamService providesLastFmStreamService(
      @Named("last_fm") Retrofit retrofit) {
    return retrofit.create(LastFmStreamService.class);
  }
}

我在app类的onCreate()中调用的应用程序注入:

mAppComponent = DaggerAristocratsAppComponent.builder()
        .aristocratsAppModule(new AristocratsAppModule(this))
        .build();

查看我在控制器中调用的注射:

getAppComponent().plus(new StreamModule(this)).inject(this);

我在构建过程中遇到的异常:

Error:(13, 8) error: [com.qsoft.streams.presentation.di.StreamComponent.inject(com.qsoft.streams.presentation.StreamsController)] com.qsoft.streams.data.network.AristocratsStreamService cannot be provided without an @Provides-annotated method.
com.qsoft.streams.data.network.AristocratsStreamService is injected at
com.qsoft.streams.data.network.StreamApi.<init>(…, streamServiceAristocrats, …)
com.qsoft.streams.data.network.StreamApi is injected at
com.qsoft.streams.data.repository.StreamRepository.<init>(streamApi, …)
com.qsoft.streams.data.repository.StreamRepository is injected at
com.qsoft.streams.domain.interactors.StreamCase.<init>(streamRepository)
com.qsoft.streams.domain.interactors.StreamCase is injected at
com.qsoft.streams.presentation.di.StreamModule.providesPresenter(streamCase, …)
com.qsoft.streams.presentation.StreamMvp.Presenter is injected at
com.qsoft.streams.presentation.StreamsController.mPresenter
com.qsoft.streams.presentation.StreamsController is injected at
com.qsoft.streams.presentation.di.StreamComponent.inject(streamsController)
A binding with matching key exists in component: com.qsoft.streams.presentation.di.StreamComponent

关于丢失@Provides-annotated method的错误投诉,但它们在我的子模块中。似乎Dagger2看不到它们。我认为问题可能在于子组件中AristocratsStreamService的缺失声明中,所以我甚至在那里添加了它但没有任何改变。

这种情况看起来很奇怪,我想听听更有经验的Dagger2开发人员的一些意见。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

我注意到你没有在AristrocratsModule中设置子组件。

此:

@Module public class AristocratsAppModule {

进入这个:

@Module(subcomponents = {StreamComponent.class})
public class AristocratsAppModule {

尝试进行以下更改。在StreamComponent中创建一个Builder。

@Subcomponent.Builder
interface Builder {
    Builder streamModule(StreamModule module);

    StreamComponent build();
}

最终,您将通过StreamComponent注入所有内容,并且它也会在AristocratsAppComponent中注入所有内容。

public StreamComponent getStreamComponent(Controller controller) {
    return getAppComponent().streamBuilder().streamModule(new StreamModule(controller)).build();
}

此后可以删除额外的东西。