【发布时间】:2022-01-20 04:52:52
【问题描述】:
test.yml(位置:资源/属性/)
edit:
field1: test
field2: test
field3: test
field4: test
PropertyConfig.kt
@Configuration
@PropertySource("classpath:properties/test.yml")
class PropertyConfig {
@Bean
@ConfigurationProperties(prefix = "edit")
fun testProperty() = mutableMapOf<String, String>()
}
@Service
class EditService(
private val testProperty: Map<String, String>
) {
fun print() {
println(testProperty) // empty
}
}
我想接收下面编辑的值作为地图。
我尝试了带有前缀和值的 @ConfigurationProperties 选项,但它不起作用。
如果我使用属性文件,它工作得很好,但不是 yml 文件。
我错过了什么?谢谢。
kotlinVersion = '1.6'; springBootVersion = '2.6.1'
【问题讨论】:
-
您是否还添加了:
@EnableConfigurationProperties(YourConfigPropClass::class) @ConfigurationPropertiesScan在您的 Application Main 顶部?
标签: spring-boot kotlin dependency-injection yaml properties-file