简体   繁体   English

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

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

I am new to Kotlin. So I was trying to write a @PropertySource class with @Value injection in Spring Boot, so that it can be used elsewhere.我是 Kotlin 的新手。所以我试图在 Spring Boot 中编写一个带有@Value注入的@PropertySource class,以便它可以在其他地方使用。

I have written the class like this:我这样写了 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
        
        .
        .
        .
        .
        .
        .
    }
}

But when I run the application, I get the error in my:但是,当我运行该应用程序时,我收到以下错误:

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

I am trying to fetch the properties in other classes like:我正在尝试获取其他类中的属性,例如:

AppProperties.appStorageType 

When I was in Java we had getters and setters to do it.当我在 Java 时,我们有 getter 和 setter 来做这件事。 What is the equivalent in Kotlin? Kotlin 中的等价物是什么?

Any help would be appreciated.任何帮助,将不胜感激。

Just initialize this properties in app-properties.properties file..只需在 app-properties.properties 文件中初始化此属性。

Create app-properties.properties file and put this line创建 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