在Kotlin中使用YAML的@Value和List
在Kotlin中使用YAML的@Value和List
我试图在一个使用Kotlin的Spring Boot项目中,从一个YAML文件初始化属性列表。对于普通字符串,它可以正常工作,但是当我尝试使用以下错误初始化List时会失败:
Unexpected exception during bean creation; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'foo.bars' in value "${foo.bars}"
。
Kotlin代码有以下构造函数参数:
@Value("\${foo.bars}") val foobars: List
。
YAML文件有以下值:
foo: bars: - test1 - test2
。
我需要在列表和普通字符串之间做一些不同的事情吗?
admin 更改状态以发布 2023年5月25日
在这里描述的解决方法可以稍微修改一下适用于Kotlin:
@Component @ConfigurationProperties("foo") class FooBarsProperties { lateinit var bars: List}
只需在需要的地方注入FooBarsProperties。不要忘记在一些配置类中添加@EnableConfigurationProperties。