error: [Dagger/MissingBinding] when trying building the project
error: [Dagger/MissingBinding] when trying building the project
我有一项服务 class,我想通过 Dagger 提供它。但我在下面收到此错误:
error: [Dagger/MissingBinding] service.KeyStoreService cannot be
provided without an @Inject constructor or an @Provides-annotated
method. service.KeyStoreService is provided at
di.component.ApplicationComponent.getKeyStoreService()
这是我的组件 class:
@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();
}
}
有人知道它哪里出了问题吗?我在 Whosebug 上查看了类似的问题,但没有找到任何对我有帮助的问题。
有一件事:Dagger 提供基于特定类型 的实例。这里有几种方法可以解决这个问题
- 将
getKeyStoreService
方法的 return 类型从 KeyStoreServiceInterface
更改为 KeyStoreModule
中的 KeyStoreService
- 将
getKeyStoreService
方法的 return 类型从 KeyStoreService
更改为 ApplicationComponent
中的 KeyStoreServiceInterface
- 创建一个带有
@Binds
注释的抽象方法,它将接收 KeyStoreService
并且 return 类型将是 KeyStoreServiceInterface
(为了做到这一点,整个模块应该是抽象 - 因此可以使用 @Binds
注释创建单独的抽象模块,或者使 KeyStoreModule
抽象并将 getKeyStoreService
方法修改为静态)
- 同样,使用
@Binds
注释将 KeyStoreService
实例映射到提供的 KeyStoreServiceInterface
,但在 KeyStoreService
构造函数上应用 @Inject
注释并提供密钥库 File
通过匕首
- 不使用
@Binds
注释,但在 KeyStoreService
构造函数上应用 @Inject
注释并通过 Dagger 提供密钥库 File
我有一项服务 class,我想通过 Dagger 提供它。但我在下面收到此错误:
error: [Dagger/MissingBinding] service.KeyStoreService cannot be provided without an @Inject constructor or an @Provides-annotated method. service.KeyStoreService is provided at di.component.ApplicationComponent.getKeyStoreService()
这是我的组件 class:
@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();
}
}
有人知道它哪里出了问题吗?我在 Whosebug 上查看了类似的问题,但没有找到任何对我有帮助的问题。
有一件事:Dagger 提供基于特定类型 的实例。这里有几种方法可以解决这个问题
- 将
getKeyStoreService
方法的 return 类型从KeyStoreServiceInterface
更改为KeyStoreModule
中的 - 将
getKeyStoreService
方法的 return 类型从KeyStoreService
更改为ApplicationComponent
中的 - 创建一个带有
@Binds
注释的抽象方法,它将接收KeyStoreService
并且 return 类型将是KeyStoreServiceInterface
(为了做到这一点,整个模块应该是抽象 - 因此可以使用@Binds
注释创建单独的抽象模块,或者使KeyStoreModule
抽象并将getKeyStoreService
方法修改为静态) - 同样,使用
@Binds
注释将KeyStoreService
实例映射到提供的KeyStoreServiceInterface
,但在KeyStoreService
构造函数上应用@Inject
注释并提供密钥库File
通过匕首 - 不使用
@Binds
注释,但在KeyStoreService
构造函数上应用@Inject
注释并通过 Dagger 提供密钥库
KeyStoreService
KeyStoreServiceInterface
File