Google Guice ProvisionException:无法提供。没有实现被绑定
Google Guice ProvisionException: Unable to provision. No implementation was bound
DI 和 guice 新手..
我想使用一个服务 (StoreLevelClient) 这是一个由其他团队定义的class。
我将这个 class 注入到我的主文件中,如下所示:
class ClientAccessor {
companion object {
private val LOGGER = KotlinLogging.logger { }
}
private val myStoreLevelClient: StoreLevelClient =
Guice.createInjector(ServiceModule()).getInstance(StoreLevelClient::class.java)
并为 StoreLevelClient 制作了一个模块文件,如下所示:
class ServiceModule : AbstractModule() {
@Provides
@Singleton
fun getClient(myServiceClient : KasServiceClient): StoreLevelClient {
return StoreLevelClient(myServiceClient, AppConfigObject.trackedDocument, AppConfigObject.appConfigFallback)
}
它给了我错误:
Caused by: com.google.inject.ProvisionException: Unable to provision, 请参阅以下错误:
3个
2022-05-20T18:27:50.800-07:00
1) 没有绑定 com.kasservice.KasServiceClient 的实现。
4个
2022-05-20T18:27:50.800-07:00
在定位 com.kasservice.KasServiceClient 时
5个
2022-05-20T18:27:50.800-07:00
对于 com.myservice.dependency.ServiceModule.getClient
的第一个参数
KasServiceClient也是别人的
所以我也在 ServiceModule 中@Provides它:
@Provides
@Singleton
fun getService(
cloudAuthCredentialVisitor: CloudAuthDefaultCredentialsVisitor,
metricsAwareCallVisitor: MetricsAwareCallVisitor,
@Named(BINGBONG_SERVICE_CLIENT_RETRY_STRATEGY)
retryStrategy: RetryStrategy<*>
): KasServiceClient {
val domain = AppConfig.findString(DOMAIN)
val realm = AppConfig.getRealm().name()
val qualifier = "$domain.$realm"
return ClientBuilder()
.remoteOf(KasServiceClient::class.java)
.withConfiguration(qualifier)
.withCallVisitors(cloudAuthCredentialVisitor, metricsAwareCallVisitor, CallAttachmentVisitor(Calls.retry(retryStrategy)))
.newClient()
}
但它给了我如下错误:
Could not find a suitable constructor in com.amazon.coral.client.cloudauth.CloudAuthDefaultCredentialsVisitor. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
Could not find a suitable constructor in com.amazon.metrics.declarative.client.MetricsAwareCallVisitor. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
CloudAuthDefaultCredentialsVisitor 和 MetricsAwareCallVisitor 已使用 @Provides 并实例化。
所以我不知道为什么guice找不到他们...??
有什么想法吗??我想知道我在使用 Guice 时有什么错误。但是我很难调试和找到
通过改变注入方式解决问题:
改为使用:
class ClientAccessor {
companion object {
private val LOGGER = KotlinLogging.logger { }
}
private val myStoreLevelClient: StoreLevelClient =
Guice.createInjector(ServiceModule()).getInstance(StoreLevelClient::class.java)
试过这个:
class ClientAccessor @Inject constructor(private val myStoreLevelClient: StoreLevelClient){
companion object {
private val LOGGER = KotlinLogging.logger { }
}
原因:
使用@Inject 而不是在特定模块上手动使用createInjector,让guice 为我们注入它。当我尝试在我的代码中直接使用 createInjector 实例化时,它只会查找指定的模块而无法加载其他模块。
DI 和 guice 新手..
我想使用一个服务 (StoreLevelClient) 这是一个由其他团队定义的class。
我将这个 class 注入到我的主文件中,如下所示:
class ClientAccessor {
companion object {
private val LOGGER = KotlinLogging.logger { }
}
private val myStoreLevelClient: StoreLevelClient =
Guice.createInjector(ServiceModule()).getInstance(StoreLevelClient::class.java)
并为 StoreLevelClient 制作了一个模块文件,如下所示:
class ServiceModule : AbstractModule() {
@Provides
@Singleton
fun getClient(myServiceClient : KasServiceClient): StoreLevelClient {
return StoreLevelClient(myServiceClient, AppConfigObject.trackedDocument, AppConfigObject.appConfigFallback)
}
它给了我错误: Caused by: com.google.inject.ProvisionException: Unable to provision, 请参阅以下错误: 3个 2022-05-20T18:27:50.800-07:00 1) 没有绑定 com.kasservice.KasServiceClient 的实现。 4个 2022-05-20T18:27:50.800-07:00 在定位 com.kasservice.KasServiceClient 时 5个 2022-05-20T18:27:50.800-07:00 对于 com.myservice.dependency.ServiceModule.getClient
的第一个参数KasServiceClient也是别人的 所以我也在 ServiceModule 中@Provides它:
@Provides
@Singleton
fun getService(
cloudAuthCredentialVisitor: CloudAuthDefaultCredentialsVisitor,
metricsAwareCallVisitor: MetricsAwareCallVisitor,
@Named(BINGBONG_SERVICE_CLIENT_RETRY_STRATEGY)
retryStrategy: RetryStrategy<*>
): KasServiceClient {
val domain = AppConfig.findString(DOMAIN)
val realm = AppConfig.getRealm().name()
val qualifier = "$domain.$realm"
return ClientBuilder()
.remoteOf(KasServiceClient::class.java)
.withConfiguration(qualifier)
.withCallVisitors(cloudAuthCredentialVisitor, metricsAwareCallVisitor, CallAttachmentVisitor(Calls.retry(retryStrategy)))
.newClient()
}
但它给了我如下错误:
Could not find a suitable constructor in com.amazon.coral.client.cloudauth.CloudAuthDefaultCredentialsVisitor. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
Could not find a suitable constructor in com.amazon.metrics.declarative.client.MetricsAwareCallVisitor. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
CloudAuthDefaultCredentialsVisitor 和 MetricsAwareCallVisitor 已使用 @Provides 并实例化。 所以我不知道为什么guice找不到他们...??
有什么想法吗??我想知道我在使用 Guice 时有什么错误。但是我很难调试和找到
通过改变注入方式解决问题:
改为使用:
class ClientAccessor {
companion object {
private val LOGGER = KotlinLogging.logger { }
}
private val myStoreLevelClient: StoreLevelClient =
Guice.createInjector(ServiceModule()).getInstance(StoreLevelClient::class.java)
试过这个:
class ClientAccessor @Inject constructor(private val myStoreLevelClient: StoreLevelClient){
companion object {
private val LOGGER = KotlinLogging.logger { }
}
原因:
使用@Inject 而不是在特定模块上手动使用createInjector,让guice 为我们注入它。当我尝试在我的代码中直接使用 createInjector 实例化时,它只会查找指定的模块而无法加载其他模块。