android – Dagger 2混合范围

前端之家收集整理的这篇文章主要介绍了android – Dagger 2混合范围前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
以前我只有一个AppComponent有四个模块(AppModule,NetworkModule,StorageModule,PresentersModule),并且到处都注入了单例.最近,我决定在我的应用程序中进行小型重构并将其划分为范围.我认为,演示者只能在活动中生活,所以我创建了@ActivityScope和ActivityModule,但由于我误解了如何混合这些范围,所以无法编译项目.
我已经在stackoverflow上阅读了很多文章和问题,但是到处都有模块独立的简单示例.在我的情况下这样的事情
@Singleton
@Component(modules = { AppModule.class,StorageModule.class,NetworkModule.class })
public interface AppComponent {
  ActivityComponent plus(PresentersModule module); // <-- error
}

不管用.我收到此错误

Error:(19,1) error: com.my.package.di.component.ActivityComponent scoped with @com.my.package.di.scope.ActivityScope may not reference bindings with different scopes:
@Provides @Singleton android.app.Application com.my.package.di.module.AppModule.provideApplication()
@Provides @Singleton com.my.package.network.FeedBurnerApi com.my.package.di.module.NetworkModule.provideFeedBurnerApi(android.app.Application)
@Provides @Singleton android.database.sqlite.sqliteOpenHelper com.my.package.di.module.StorageModule.providesqliteOpenHelper(android.app.Application)
@Provides @Singleton com.my.package.storage.Repository com.my.package.di.module.StorageModule.provideRepository(android.database.sqlite.sqliteOpenHelper)
@Provides @Singleton com.my.package.SharedPreferencesHelper com.my.package.di.module.StorageModule.provideSharedPreferencesHelper(android.app.Application)

那么,问题是如何获取ActivityComponent的实例?

您可以在下面看到模块之间的依赖关系:

申请模块:

@Module
public final class AppModule {
  private final MyApplication mApplication;

  public AppModule(MyApplication application) { ... }

  @Provides @Singleton Application provideApplication() { ... }
}

网络模块:

@Module(includes = { AppModule.class })
public final class NetworkModule {
  @Provides @Singleton FeedBurnerApi provideFeedBurnerApi(Application application) { ... }

  @Provides @Singleton Retrofit provideRetrofit() { ... }
}

存储模块:

@Module(includes = { AppModule.class })
public final class StorageModule {
  @Provides @Singleton Repository provideRepository(sqliteOpenHelper sqliteOpenHelper) { ... }

  @Provides @Singleton sqliteOpenHelper providesqliteOpenHelper(Application application) { ... }

  @Provides @Singleton SharedPreferencesHelper provideSharedPreferencesHelper(Application application) { ... }
}

演示者模块:

@Module(includes = { AppModule.class,NetworkModule.class,StorageModule.class })
public final class PresentersModule {
  @Provides FeedPageViewPresenter provideFeedPageViewPresenter(FeedBurnerApi api,Repository repository,SharedPreferencesHelper preferences) { ... }

  @Provides @ActivityScope SlidingTabsViewPresenter provideSlidingTabsViewPresenter(Repository repository) { ... }
}

申请部分:

@Singleton
@Component(modules = { AppModule.class,NetworkModule.class })
public interface AppComponent {}

活动组件:

@Subcomponent(modules = PresentersModule.class)
@ActivityScope
public interface ActivityComponent {
  void inject(FeedPageView view);

  void inject(SlidingTabsView view);
}

解决方法

问题发生在我的PresentersModule中.

由于子组件可以从父项访问整个对象图,因此我不需要在子模块中包含这些依赖项.
所以,我改变了这段代码

@Module(includes = { AppModule.class,StorageModule.class })
public final class PresentersModule { ... }

有了这个:

@Module
public final class PresentersModule { ... }

解决了我的问题.

原文链接:https://www.f2er.com/android/309780.html

猜你在找的Android相关文章