错误:找到了依赖循环

时间:2018-03-05 20:48:52

标签: android dependency-injection dagger-2

我正在尝试注入ItemRepository,这取决于RemoteDataSourceLocalDataSource。 我花了很多时间来解决问题,但没有运气。

 Error:(26, 10) error: Found a dependency cycle:
com.project.bishoy.lost.data.ItemsRepository is injected at
com.project.bishoy.lost.di.modules.ApplicationModule.provideItemsRepository(itemsRepository)
com.project.bishoy.lost.data.ItemsRepository is injected at
com.project.bishoy.lost.LostApp.mItemsRepository
com.project.bishoy.lost.LostApp is injected at
com.project.bishoy.lost.di.components.ApplicationComponent.inject(lostApp)
Error:(34, 21) error: Found a dependency cycle:
com.project.bishoy.lost.data.local.LocalDataSource is injected at
com.project.bishoy.lost.di.modules.ApplicationModule.provideLocalDataSource(localDataSource)
com.project.bishoy.lost.data.local.LocalDataSource is provided at
com.project.bishoy.lost.di.components.ApplicationComponent.getLocalDataSource()
Error:(36, 22) error: Found a dependency cycle:
com.project.bishoy.lost.data.remote.RemoteDataSource is injected at
com.project.bishoy.lost.di.modules.ApplicationModule.provideRemoteDataSource(remoteDataSource)
com.project.bishoy.lost.data.remote.RemoteDataSource is provided at
com.project.bishoy.lost.di.components.ApplicationComponent.getRemoteDataSource()
Error:(38, 21) error: Found a dependency cycle:
com.project.bishoy.lost.data.ItemsRepository is injected at
com.project.bishoy.lost.di.modules.ApplicationModule.provideItemsRepository(itemsRepository)
com.project.bishoy.lost.data.ItemsRepository is provided at
com.project.bishoy.lost.di.components.ApplicationComponent.getDataManager()

ApplicationModule.java

@Module
public class ApplicationModule {

 private final Application mApplication;

 public ApplicationModule(Application app) {
  mApplication = app;
 }

 @Provides
 @ApplicationContext
 Context provideContext() {
  return mApplication;
 }

 @Provides
 Application provideApplication() {
  return mApplication;
 }


 @Provides
 @Singleton
 RemoteDataSource provideRemoteDataSource(RemoteDataSource remoteDataSource) {
  return remoteDataSource;
 }

 @Provides
 @Singleton
 LocalDataSource provideLocalDataSource(LocalDataSource localDataSource) {
  return localDataSource;
 }

 @Singleton
 @Provides
 ItemsRepository provideItemsRepository(ItemsRepository itemsRepository) {
  return itemsRepository;
 }
}

ApplicationComponent.java

@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {

 void inject(LostApp lostApp);

 Application getApplication();

 @ApplicationContext
 Context getContext();

 LocalDataSource getLocalDataSource();
 RemoteDataSource getRemoteDataSource();
 ItemsRepository getDataManager();
}

1 个答案:

答案 0 :(得分:1)

假设您在RemoteDataSource.javaLocalDataSource.java上没有参数的简单构造函数,并且您有一个构造函数接收ItemRepository.java中的本地和远程数据源,您的代码看起来就像这样:

@Module
public class ApplicationModule {

 private final Application mApplication;

 public ApplicationModule(Application app) {
  mApplication = app;
 }

 @Provides
 @ApplicationContext
 Context provideContext() {
  return mApplication;
 }

 @Provides
 Application provideApplication() {
  return mApplication;
 }

 @Provides
 @Singleton
 RemoteDataSource provideRemoteDataSource() {
  return new RemoteDataSource();
 }

 @Provides
 @Singleton
 LocalDataSource provideLocalDataSource() {
  return new LocalDataSource();
 }

 @Singleton
 @Provides
 ItemsRepository provideItemsRepository(LocalDataSource localDataSource, RemoteDataSource remoteDataSource) {
  return new ItemsRepository(localDataSource, remoteDataSource);
 }
}