【发布时间】:2019-04-23 10:21:18
【问题描述】:
我有一个服务类,我想通过 Dagger 提供它。但我在下面得到这个错误:
错误:[Dagger/MissingBinding] service.KeyStoreService 不能 在没有 @Inject 构造函数或 @Provides-annotated 的情况下提供 方法。 service.KeyStoreService 提供于 di.component.ApplicationComponent.getKeyStoreService()
这是我的组件类:
@ApplicationScope
@Component(modules = {ApplicationContextModule.class, KeyStoreModule.class})
public interface ApplicationComponent {
@ApplicationContext
Context getApplicationContext();
KeyStoreService getKeyStoreService();
}
这是我的 KeyStoreModule:
@Module(includes = {ApplicationContextModule.class})
public class KeyStoreModule {
@Provides
@ApplicationScope
KeyStoreServiceInterface getKeyStoreService(@ApplicationScope Context context){
File file = new File(context.getFilesDir(), "keystore/keystore");
return new KeyStoreService(file);
}
}
KeyStoreService 实现 KeyStoreServiceInterface。
这是我如何启动 Dagger2:
public class MyApplication extends Application {
private ApplicationComponent applicationComponent;
@Override
public void onCreate() {
super.onCreate();
applicationComponent = DaggerApplicationComponent.builder()
.applicationContextModule(new ApplicationContextModule(this))
.build();
}
}
有人知道哪里出了问题吗?我在 Stackoverflow 上查看了类似的问题,但没有找到任何对我有帮助的东西。
【问题讨论】:
标签: java android dependency-injection dagger-2 dagger