Kotlin & Spring Boot @ConfigurationProperties

2020-05-29 13:15发布

问题:

How to properly initialize ConfigurationProperties in Spring Boot with Kotlin?

Currently I do like in the example below:

 @ConfigurationProperties("app")
 class Config {
     var foo: String? = null
 }

But it looks pretty ugly and actually foo is not a variable, foo is constant value and should be initialized during startup and will not change in the future.

回答1:

With new Spring Boot 2.2 you can do like so:

@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:

dependencies {
  annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
}


回答2:

Here is how I have it working with my application.yml file.

myconfig:
  my-host: ssl://example.com
  my-port: 23894
  my-user: user
  my-pass: pass

Here is the kotlin file:

@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.



回答3:

Update: As of Spring Boot 2.2.0, you can use data classes as follows:

@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

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 val is not possible at the moment.

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 [...]

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



回答4:

@Value("\${some.property.key:}")
lateinit var foo:String

could be used this way



回答5:

application.properties

metro.metro2.url= ######

Metro2Config.kt

@Component
@ConfigurationProperties(prefix = "metro")
data class Metro2PropertyConfiguration(

        val metro2: Metro2 = Metro2()
)

data class Metro2(
    var url: String ?= null
)

build.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"



回答6:

This is how I did it:

application.properties

my.prefix.myValue=1

MyProperties.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

@Component
class MyService(val myProperties: MyProperties) {
    fun doIt() {
        System.console().printf(myProperties.getMyValue().toString())
    }
}