[英]error: [Dagger/MissingBinding] when trying building the project
I have a service class and I want to provide it via Dagger. 我有一个服务类,我想通过Dagger提供它。 But I get this error below: 但是我在下面得到这个错误:
error: [Dagger/MissingBinding] service.KeyStoreService cannot be provided without an @Inject constructor or an @Provides-annotated method. 错误:[Dagger / MissingBinding] service.KeyStoreService如果没有@Inject构造函数或@Provides注释的方法,将无法提供。 service.KeyStoreService is provided at di.component.ApplicationComponent.getKeyStoreService() 在di.component.ApplicationComponent.getKeyStoreService()处提供了service.KeyStoreService
Here is my component class: 这是我的组件类:
@ApplicationScope
@Component(modules = {ApplicationContextModule.class, KeyStoreModule.class})
public interface ApplicationComponent {
@ApplicationContext
Context getApplicationContext();
KeyStoreService getKeyStoreService();
}
Here is my KeyStoreModule: 这是我的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 implements KeyStoreServiceInterface. KeyStoreService实现KeyStoreServiceInterface。
Here is how I start Dagger2: 这是我启动Dagger2的方法:
public class MyApplication extends Application {
private ApplicationComponent applicationComponent;
@Override
public void onCreate() {
super.onCreate();
applicationComponent = DaggerApplicationComponent.builder()
.applicationContextModule(new ApplicationContextModule(this))
.build();
}
}
Anyone see where it could have gone wrong? 有人看到它可能出问题了吗? I looked at similar questions on Stackoverflow but did not find anything that helped me. 我在Stackoverflow上查看了类似的问题,但没有发现任何对我有帮助的东西。
Here is a thing: Dagger provides instances based on specific type . 这件事:Dagger提供基于特定类型的实例。 Here are a couple of ways to deal with this problem 以下是解决此问题的几种方法
getKeyStoreService
method from KeyStoreServiceInterface
to KeyStoreService
in KeyStoreModule
将getKeyStoreService
方法的返回类型从KeyStoreServiceInterface
更改为KeyStoreService
中的KeyStoreModule
getKeyStoreService
method from KeyStoreService
to KeyStoreServiceInterface
in ApplicationComponent
将getKeyStoreService
方法的返回类型从KeyStoreService
更改为ApplicationComponent
KeyStoreServiceInterface
@Binds
annotation which will receive KeyStoreService
and which return type will be KeyStoreServiceInterface
(in order to do this the whole module should be abstract - so it is possible to either create separate abstract module with @Binds
annotations or to make KeyStoreModule
abstract and modify getKeyStoreService
method to be static) 创建带有@Binds
批注的抽象方法,该方法将接收KeyStoreService
,返回类型为KeyStoreServiceInterface
(为此,整个模块应该是抽象的-因此可以使用@Binds
批注创建单独的抽象模块,也可以制作KeyStoreModule
将getKeyStoreService
方法抽象并修改为静态) @Binds
annotation to map KeyStoreService
instance to the provided KeyStoreServiceInterface
, but applying @Inject
annotation on KeyStoreService
constructor and provide keystore File
through Dagger 同样,使用@Binds
批注将KeyStoreService
实例映射到提供的KeyStoreServiceInterface
,但在KeyStoreService
构造函数上应用@Inject
批注并通过Dagger提供密钥库File
@Binds
annotation, but applying @Inject
annotation on KeyStoreService
constructor and provide keystore File
through Dagger 不使用@Binds
批注,而是在KeyStoreService
构造函数上应用@Inject
批注,并通过Dagger提供密钥库File
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.