Kotlin 和 Spring Boot @ConfigurationProperties
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45953118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Kotlin & Spring Boot @ConfigurationProperties
提问by Sonique
How to properly initialize ConfigurationProperties in Spring Bootwith Kotlin?
如何使用Kotlin在Spring Boot 中正确初始化 ConfigurationProperties ?
CurrentlyI do like in the example below:
目前我喜欢下面的例子:
@ConfigurationProperties("app")
class Config {
var foo: String? = null
}
But it looks pretty ugly and actually foois not a variable, foo is constantvalue and should be initialized during startup and will not change in the future.
但它看起来很丑,实际上foo不是一个variable, foo 是常量value 并且应该在启动期间初始化并且将来不会改变。
采纳答案by Dmitry Kaltovich
With new Spring Boot 2.2you can do like so:
使用新的Spring Boot 2.2,您可以这样做:
@ConstructorBinding
@ConfigurationProperties(prefix = "swagger")
data class SwaggerProp(
val title: String, val description: String, val version: String
)
And don't forget to include this in your dependencies in build.gradle.kts:
并且不要忘记将其包含在您的依赖项中build.gradle.kts:
dependencies {
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
}
回答by Ray Hunter
Here is how I have it working with my application.yml file.
这是我如何处理我的 application.yml 文件。
myconfig:
my-host: ssl://example.com
my-port: 23894
my-user: user
my-pass: pass
Here is the kotlin file:
这是 kotlin 文件:
@Configuration
@ConfigurationProperties(prefix = "myconfig")
class MqttProperties {
lateinit var myHost: String
lateinit var myPort: String
lateinit var myUser: String
lateinit var myPass: String
}
This worked great for me.
这对我很有用。
回答by s1m0nw1
Update: As of Spring Boot 2.2.0, you can use data classes as follows:
更新:从 Spring Boot 2.2.0 开始,您可以按如下方式使用数据类:
@ConstructorBinding
@ConfigurationProperties("example.kotlin")
data class KotlinExampleProperties(
val name: String,
val description: String,
val myService: MyService) {
data class MyService(
val apiToken: String,
val uri: URI
)
}
For further reference, see the official documentation.
如需进一步参考,请参阅官方文档。
Obsolete as of Spring Boot 2.2.0, Issue closed
自 Spring Boot 2.2.0 起已过时,问题已关闭
As stated in the docs: A "Java Bean“ has to be provided in order to use ConfigurationProperties. This means your properties need to have getters and setters, thus valis not possible at the moment.
如文档中所述:必须提供“ Java Bean”才能使用ConfigurationProperties。这意味着您的属性需要具有 getter 和 setter,因此val目前无法实现。
Getters and setters are usually mandatory, since binding is via standard Java Beans property descriptors, just like in Spring MVC. There are cases where a setter may be omitted [...]
Getter 和 setter 通常是强制性的,因为绑定是通过标准的 Java Beans 属性描述符进行的,就像在 Spring MVC 中一样。在某些情况下可以省略 setter [...]
This has been resolved for Spring Boot 2.2.0, which is supposed to be released soon: https://github.com/spring-projects/spring-boot/issues/8762
Spring Boot 2.2.0 已经解决了这个问题,应该很快就会发布:https: //github.com/spring-projects/spring-boot/issues/8762
回答by StanislavL
@Value("${some.property.key:}")
lateinit var foo:String
could be used this way
可以这样使用
回答by EJJ
application.properties
应用程序属性
metro.metro2.url= ######
Metro2Config.kt
Metro2Config.kt
@Component
@ConfigurationProperties(prefix = "metro")
data class Metro2PropertyConfiguration(
val metro2: Metro2 = Metro2()
)
data class Metro2(
var url: String ?= null
)
build.gradle
构建.gradle
Plugins:
id 'org.jetbrains.kotlin.kapt' version '1.2.31'
// kapt dependencies required for IntelliJ auto complete of kotlin config properties class
kapt "org.springframework.boot:spring-boot-configuration-processor"
compile "org.springframework.boot:spring-boot-configuration-processor"
回答by Qutory
This is how I did it:
我是这样做的:
application.properties
应用程序属性
my.prefix.myValue=1
MyProperties.kt
我的属性.kt
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.stereotype.Component
@Component
@ConfigurationProperties(prefix = "my.prefix")
class MyProperties
{
private var myValue = 0
fun getMyValue(): Int {
return myValue;
}
fun setMyValue(value: Int){
myValue = value
}
}
MyService.kt
我的服务.kt
@Component
class MyService(val myProperties: MyProperties) {
fun doIt() {
System.console().printf(myProperties.getMyValue().toString())
}
}

