Difficulty in @Value Injection in @PropertySource in Kotlin/SpringBoot

211 Views Asked by At

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.

I have written the class like this:

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. What is the equivalent in Kotlin?

Any help would be appreciated.

1

There are 1 best solutions below

1
On

Just initialize this properties in app-properties.properties file..

Create app-properties.properties file and put this line

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