[英]Play 2.5.12 getting error ProvisionException: Unable to provision No implementation for com.typesafe.config.Config was bound
[英]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, see the following errors: 3 2022-05-20T18:27:50.800-07:00 1) No implementation for 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.getClientModule 的第一個參數
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 進行實例化時,它只會查找指定的模塊而無法加載其他模塊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.