我有这个自定义对象:
data class Pair(
var first: String = "1",
var second: String = "2"
)
现在我想用我的自动装配它application.yml
:
my-properties:
my-integer-list:
- 1
- 2
- 3
my-map:
- "abc": "123"
- "test": "test"
pair:
first: "abc"
second: "123"
使用这个类:
@Configuration
@ConfigurationProperties(prefix = "my-properties")
class ComplexProperties {
lateinit var myIntegerList: List<Int>
lateinit var myMap: Map<String, String>
lateinit var pair: Pair
}
在添加之前Pair
它工作正常,但在我得到之后Reason: lateinit property pair has not been initialized
这是我的main
:
@SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}
@RestController
class MyRestController(
val props: ComplexProperties
) {
@GetMapping
fun getProperties(): String {
println("myIntegerList: ${props.myIntegerList}")
println("myMap: ${props.myMap}")
println("pair: ${props.pair}")
return "hello world"
}
}
使用 java 我已经完成了这个,但是我看不到这里缺少什么。