如何使用 Kotlin 在 Spring Boot 中正确初始化 ConfigurationProperties?
目前我喜欢下面的例子:
@ConfigurationProperties("app")
class Config {
var foo: String? = null
}
但它看起来很丑,实际上 foo
不是 var
iable,foo 是 constant val
ue 应该在启动时初始化,以后不会改变。
最佳答案
使用新的 Spring Boot 2.2,您可以这样做:
@ConstructorBinding
@ConfigurationProperties(prefix = "swagger")
data class SwaggerProp(
val title: String, val description: String, val version: String
)
并且不要忘记将它包含在 build.gradle.kts
的依赖项中:
dependencies {
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
}
关于spring - Kotlin 和 Spring Boot @ConfigurationProperties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45953118/
相关文章:
variables - 在 Kotlin 中一次定义多个变量(例如 Java : String x,
gradle - 如何使用 kotlinscript DSL (build.gradle.kts)
constructor - Kotlin 数据类可以有多个构造函数吗?
android - RealmObject 的 Kotlin 数据类
java - 为什么以及何时将 @JvmStatic 与伴随对象一起使用?
android - 警告 "Kotlin plugin version is not the sam
android - Kotlin:const val 与 val
android - 更新到 Kotlin 1.3.30 会破坏 Dagger 2.21 的构建