Kotlin& Spring Boot @ConfigurationProperties

时间:2017-08-30 06:09:32

标签: spring spring-boot kotlin

如何使用 Kotlin Spring Boot 中正确初始化ConfigurationProperties?

目前我喜欢以下示例:

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

但它看起来很丑陋,实际上foo不是var iable,foo是常量 val ue,应该在启动时初始化并且将来不会改变

9 个答案:

答案 0 :(得分:26)

以下是我使用application.yml文件的方法。

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

这是kotlin文件:

@Configuration
@ConfigurationProperties(prefix = "myconfig")
class MqttProperties {
    lateinit var myHost: String
    lateinit var myPort: String
    lateinit var myUser: String
    lateinit var myPass: String    
}

这对我很有用。

答案 1 :(得分:10)

docs中所述:必须提供A" Java Bean 才能使用ConfigurationProperties。这意味着您的属性需要包含getter和setter,因此目前无法val

  

getter和setter通常是必需的,因为绑定是通过标准的Java Beans属性描述符,就像在Spring MVC中一样。有些情况下可能会省略setter [...]

这已经解决了Spring Boot 2.2.0,它应该很快就会发布: https://github.com/spring-projects/spring-boot/issues/8762

答案 2 :(得分:10)

使用新的 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")
}

答案 3 :(得分:2)

var attributedString = try? NSAttributedString(data: inst.desc.data(using: String.Encoding.unicode), options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
var newString = NSMutableAttributedString(attributedString: attributedString!)
var range: NSRange = [0, newString.length]
newString.enumerateAttribute(NSFontAttributeName, in: range, options: .longestEffectiveRangeNotRequired, using: {(_ value: Any, _ range: NSRange, _ stop: Bool) -> Void in
    var replacementFont = UIFont(name: "Palatino-Roman", size: 14.0)
    newString.addAttribute(NSFontAttributeName, value: replacementFont!, range: range)
})
label.attributedText = newString

可以这样使用

答案 4 :(得分:1)

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"

答案 5 :(得分:1)

在带有 Kotlin 1.4.3 的 Spring Boot 2.4.3 上,下一个方法不再有效(可能是因为错误):

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.EnableConfigurationProperties

@SpringBootApplication
@EnableConfigurationProperties(TestProperties::class)
class Application

fun main(args: Array<String>) {
    runApplication<Application>(*args)
}
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.ConstructorBinding

@ConfigurationProperties(prefix = "test")
@ConstructorBinding
data class TestProperties(
    val value: String
)

上面的代码在暗示接下来的两种方法之一后开始工作:

  1. 添加依赖
implementation("org.jetbrains.kotlin:kotlin-reflect")
  1. 更新属性类
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.ConstructorBinding

@ConfigurationProperties(prefix = "test")
data class TestProperties @ConstructorBinding constructor(
    val value: String
)

问题发生在行 org/springframework/boot/context/properties/ConfigurationPropertiesBindConstructorProvider.java#68

答案 6 :(得分:0)

我就这样做了:

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())
    }
}

答案 7 :(得分:0)

@ConstructorBinding
@ConfigurationProperties(prefix = "your.prefix")
data class AppProperties (
    val invoiceBaseDir: String,
    val invoiceOutputFolderPdf: String,
    val staticFileFolder: String
)

别忘了添加@ConfigurationPropertiesScan

@ConfigurationPropertiesScan
class Application

fun main(args: Array<String>) {
    runApplication<Application>(*args)
}

最后是application.properties文件:

your.prefix.invoiceBaseDir=D:/brot-files
your.prefix.invoiceOutputFolderPdf=invoices-pdf
your.prefix.staticFileFolder=static-resources

答案 8 :(得分:0)

除了已经说过的内容之外,请注意 ExecuteStoredProcSingleRowval 有一些限制。您不能将一个变量别名为另一个。假设您在 Kubernetes 中运行并想要捕获主机名,该主机名由 env var @ConstructorBinding 给出。执行此操作的最简单方法是将 HOSTNAME 应用于属性,但它仅适用于可变属性且没有构造函数绑定。

@ConstructorBinding with immutable properties don't work with @Value in Spring Boot Kotlin @ConfigurationProperties