如果没有@Provides 注释方法,则无法提供 Dagger 2 LocalDataSource
Dagger 2 LocalDataSource cannot be provided without @Provides annotated method
我正在尝试通过使用 DI 创建存储库模式来遵循与 here 中相同的示例。
问题是我收到以下错误:
"Error:(16, 20) error: @mvp.model.di.scope.Local
mvp.model.repository.local.GameLocalDataSource cannot be provided
without an @Provides-annotated method. @mvp.model.di.scope.Local
mvp.model.repository.local.GameLocalDataSource is injected at
mvp.model.repository.GameRepository.(gameLocalDataSource, …)
mvp.model.repository.GameRepository is provided at
mvp.model.di.component.RepositoryComponent.getGameRepository()"
这是与应用相关的代码:
public class GameApplication extends Application {
private RepositoryComponent repositoryComponent;
@Override
public void onCreate() {
super.onCreate();
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
LeakCanary.install(this);
// Normal app init code...
repositoryComponent = DaggerRepositoryComponent.builder()
.applicationModule(new ApplicationModule((getApplicationContext())))
.build();
}
public RepositoryComponent getRepositoryComponent() {
return repositoryComponent;
}
}
这是我的 RepositoryComponent
:
@Singleton
@Component(modules = {RepositoryModule.class, ApplicationModule.class})
public interface RepositoryComponent {
GameRepository getGameRepository();
}
这是RepositoryModule
:
@Module
public class RepositoryModule {
@Singleton
@Provides
@Local
GameDataSource provideLocalDataSource(Context context) {
return new GameLocalDataSource(context);
}
@Singleton
@Provides
@Remote
GameDataSource provideRemoteDataSource() {
return new GameRemoteDataSource();
}
}
最后,ApplicationModule
:
@Module
public final class ApplicationModule {
private Context context;
public ApplicationModule(Context context) {
this.context = context;
}
@Provides
Context providesContext() {
return context;
}
}
这是我的大部分 GameRepository
class:
@Singleton
public class GameRepository implements GameDataSource {
private GameDataSource remoteDataSource;
private GameDataSource localDataSource;
@Inject
public GameRepository(@Local GameLocalDataSource gameLocalDataSource, @Remote GameRemoteDataSource gameRemoteDataSource) {
remoteDataSource = gameRemoteDataSource;
localDataSource = gameLocalDataSource;
}
此外,如上述示例所示,我创建了几个范围,@Local
和 @Remote
,因为我的两个数据源具有相同的类型,Dagger 需要区分它们。
@Qualifier
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Local {
}
我与 dagger 相关的其余代码,只是构造函数中我想注入依赖项的 @Inject
。
此外,DaggerRepositoryComponent
永远不会在 GameApplication
class.
中生成
非常感谢您的提前帮助!
GameLocalDataSource cannot be provided without an @Provides-annotated method
您在代码中的某处尝试 @Inject
GameLocalDataSource
,但您已在模块中指定如何提供 GameDataSource
, 而不是 GameLocalDataSource
.
...
GameDataSource provideLocalDataSource(Context context) {
return new GameLocalDataSource(context);
}
...
要么让Dagger注入GameDataSource
,要么描述Dagger如何提供GameLocalDataSource
。
...
GameLocalDataSource provideLocalDataSource(Context context) {
return new GameLocalDataSource(context);
}
...
我正在尝试通过使用 DI 创建存储库模式来遵循与 here 中相同的示例。 问题是我收到以下错误:
"Error:(16, 20) error: @mvp.model.di.scope.Local mvp.model.repository.local.GameLocalDataSource cannot be provided without an @Provides-annotated method. @mvp.model.di.scope.Local mvp.model.repository.local.GameLocalDataSource is injected at mvp.model.repository.GameRepository.(gameLocalDataSource, …) mvp.model.repository.GameRepository is provided at mvp.model.di.component.RepositoryComponent.getGameRepository()"
这是与应用相关的代码:
public class GameApplication extends Application {
private RepositoryComponent repositoryComponent;
@Override
public void onCreate() {
super.onCreate();
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
LeakCanary.install(this);
// Normal app init code...
repositoryComponent = DaggerRepositoryComponent.builder()
.applicationModule(new ApplicationModule((getApplicationContext())))
.build();
}
public RepositoryComponent getRepositoryComponent() {
return repositoryComponent;
}
}
这是我的 RepositoryComponent
:
@Singleton
@Component(modules = {RepositoryModule.class, ApplicationModule.class})
public interface RepositoryComponent {
GameRepository getGameRepository();
}
这是RepositoryModule
:
@Module
public class RepositoryModule {
@Singleton
@Provides
@Local
GameDataSource provideLocalDataSource(Context context) {
return new GameLocalDataSource(context);
}
@Singleton
@Provides
@Remote
GameDataSource provideRemoteDataSource() {
return new GameRemoteDataSource();
}
}
最后,ApplicationModule
:
@Module
public final class ApplicationModule {
private Context context;
public ApplicationModule(Context context) {
this.context = context;
}
@Provides
Context providesContext() {
return context;
}
}
这是我的大部分 GameRepository
class:
@Singleton
public class GameRepository implements GameDataSource {
private GameDataSource remoteDataSource;
private GameDataSource localDataSource;
@Inject
public GameRepository(@Local GameLocalDataSource gameLocalDataSource, @Remote GameRemoteDataSource gameRemoteDataSource) {
remoteDataSource = gameRemoteDataSource;
localDataSource = gameLocalDataSource;
}
此外,如上述示例所示,我创建了几个范围,@Local
和 @Remote
,因为我的两个数据源具有相同的类型,Dagger 需要区分它们。
@Qualifier
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Local {
}
我与 dagger 相关的其余代码,只是构造函数中我想注入依赖项的 @Inject
。
此外,DaggerRepositoryComponent
永远不会在 GameApplication
class.
非常感谢您的提前帮助!
GameLocalDataSource cannot be provided without an @Provides-annotated method
您在代码中的某处尝试 @Inject
GameLocalDataSource
,但您已在模块中指定如何提供 GameDataSource
, 而不是 GameLocalDataSource
.
...
GameDataSource provideLocalDataSource(Context context) {
return new GameLocalDataSource(context);
}
...
要么让Dagger注入GameDataSource
,要么描述Dagger如何提供GameLocalDataSource
。
...
GameLocalDataSource provideLocalDataSource(Context context) {
return new GameLocalDataSource(context);
}
...