Home > front end >  declare global variable in kotlin defaulted to a property value
declare global variable in kotlin defaulted to a property value

Time:10-25

I am trying to load a global variable in kotlin directly from the application.yml:

telegram:
  token: foo

to achieve this, in my class I've tried this:

@Value("\${telegram.token}")
val botToken: String

But it is throwing an error saying that I need to initialize the property. (For example, this doesn't throw an error but it is not my expected behaviour):

@Value("\${telegram.token}")
val botToken: String = ""

What I want is to inject the config value (foo) into this constant (botToken).

CodePudding user response:

Either add it as a parameter in the constructor of the bean that contains the property or try the following:

@Value("\${telegram.token}")
lateinit var botToken: String

CodePudding user response:

first of all it seems like you could use @Property(name = "telegram.token")

and then i would attempt

@Property(name = "telegram.token")
lateinit var token: String
    private set
  • Related