Kotlin application.yml 数据 class 寻找 beans

Kotlin application.yml data class looking for beans

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

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

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

@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 属性的示例看起来都与我的数据相同 class.

Spring-Boot v2.3.0.RELEASE 科特林 v1.3.72

原来我缺少依赖项

org.jetbrains.kotlin:kotlin-reflect

文档: https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/htmlsingle/#boot-features-kotlin-requirements

问题: https://github.com/spring-projects/spring-boot/issues/19582