简体   繁体   中英

Spring nested ConfigurationProperties and Kotlin

I want to use nested ConfigurationProperties in spring boot which is works in java but not in kotlin. I have got the following lines in my app property:

cert.signing-cert.filePath=truststore.jks
cert.signing-cert.password=xxxx
cert.private-signing.filePath=sign_test.p12
cert.private-signing.password=xxxx
cert.private-encrpytion.filePath=encryption_test.p12
cert.private-encrpytion.password=xxx

And I created this structure to handle these properties:

@Component
@ConfigurationProperties("cert")
class CertConfig {
    lateinit var signingCert: SigningCert
    lateinit var privateSigning: PrivateSigning
    lateinit var privateEncrpytion: PrivateEncrpytion

    class SigningCert {
        lateinit var filePath: String
        lateinit var password: String
    }

    class PrivateSigning {
        lateinit var filePath: String
        lateinit var password: String
    }

    class PrivateEncrpytion {
        lateinit var filePath: String
        lateinit var password: String
    }
}

I added kapt plugin and dependecy to my build.gradle.kts

    kotlin("kapt") version "1.3.50"
...
    kapt("org.springframework.boot:spring-boot-configuration-processor")
    implementation("org.springframework.boot:spring-boot-configuration-processor")

And I got this exception when I run this code:

Caused by: kotlin.UninitializedPropertyAccessException: lateinit property signingCert has not been initialized

I forgot to initialize the properties.

 var signingCert: SigningCert = SigningCert()
 var privateSigning: PrivateSigning = PrivateSigning()
 var privateEncrpytion: PrivateEncrpytion = PrivateEncrpytion()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM