簡體   English   中英

Kotlin/SpringBoot 中@PropertySource 中的@Value 注入困難

[英]Difficulty in @Value Injection in @PropertySource in Kotlin/SpringBoot

我是 Kotlin 的新手。所以我試圖在 Spring Boot 中編寫一個帶有@Value注入的@PropertySource class,以便它可以在其他地方使用。

我這樣寫了 class:

import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.PropertySource


@Configuration
@PropertySource("classpath:app-properties.properties")
class AppProperties {

    companion object {

        @Value("\${app.storage.types}")
        lateinit var appStorageType : String

        @Value("\${app.device.supported-protocols}")
        lateinit var appDeviceSupportedProtocols : String
        
        .
        .
        .
        .
        .
        .
    }
}

但是,當我運行該應用程序時,我收到以下錯誤:

{
    "error": {
        "message": [
            "lateinit property appStorageType has not been initialized"
        ]
    }
}

我正在嘗試獲取其他類中的屬性,例如:

AppProperties.appStorageType 

當我在 Java 時,我們有 getter 和 setter 來做這件事。 Kotlin 中的等價物是什么?

任何幫助,將不勝感激。

只需在 app-properties.properties 文件中初始化此屬性。

創建 app-properties.properties 文件並將此行

app.storage.types.appStorageType=some value
app.device.supported-protocols.appDeviceSupportedProtocols=other value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM