YAML中Kotlin中的@Value与列表

时间:2019-03-15 08:07:17

标签: spring kotlin yaml

我正在尝试使用Kotlin在Spring Boot项目中初始化YAML文件中的属性列表。

它对于普通字符串正常工作,但是当我尝试初始化具有以下错误的列表时失败:

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<String>

yaml文件具有以下值:

foo:
  bars:
  - test1
  - test2

我需要在列表和普通字符串之间做些不同的事情吗?

1 个答案:

答案 0 :(得分:0)

对于here描述的解决方案可以对Kotlin进行一些更改:

@Component
@ConfigurationProperties("foo")
class FooBarsProperties {
  lateinit var bars: List<String>
}

只需在需要的地方注入 FooBarsProperties 。不要忘记在某些配置类中添加 @EnableConfigurationProperties