【问题标题】:Kotlin application.yml data class looking for beansKotlin application.yml 数据类寻找 bean
【发布时间】:2020-10-29 19:16:28
【问题描述】:

我有一个 Kotlin 数据类,我想从 application.yml 文件中读取属性。这是我的数据类:

@ConstructorBinding
@ConfigurationProperties("meanwhile.in.hell.myapp")
data class MyAppProperties(val serverId: String, val locationId: String)

然后我将它添加到我的配置类中:

@Configuration
@EnableConfigurationProperties(MyAppProperties::class)
open class MyAppConfiguration(private val properties: MyAppProperties) {

我只使用properties.serverId 访问值并将对象传递给正在创建的其他bean 的构造函数,例如这个:

open class MyAppClient(
    private val webClient: WebClient,
    private val properties: MyAppProperties
) : IMyAppClient {

但是,当我启动我的应用程序时,我收到一个错误,它没有尝试从 application.yml 加载属性,而是尝试为构造函数参数查找 bean:

Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'meanwhile.in.hell.myapp-meanwhile.in.hell.myapp.MyAppProperties': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Parameter 0 of constructor in meanwhile.in.hell.myapp.MyAppProperties required a bean of type 'java.lang.String' that could not be found.

如何阻止我的应用认为这些参数是自动装配的?我知道在 Kotlin 中,这就是构造函数 Autowired bean 的样子(即,不需要注释),但是我在网上看到的所有关于如何读取 application.yml 属性的示例看起来都与我的数据类相同。

Spring-Boot v2.3.0.RELEASE Kotlin v1.3.72

【问题讨论】:

    标签: spring spring-boot kotlin spring-properties configurationproperties


    【解决方案1】:
    猜你喜欢
    • 2016-06-21
    • 1970-01-01
    • 2018-10-01
    • 2011-10-10
    • 2010-10-11
    • 2017-05-23
    • 2019-08-16
    • 2014-11-17
    • 2022-06-30
    相关资源
    最近更新 更多