How to Use @Value in Spring Boot with Kotlin

Photo by Nadezhda Filatova on Unsplash
Photo by Nadezhda Filatova on Unsplash
If your project is written in Java, you can use @Value(“${name}”) to get the value of my.food. However, if it is written in Kotllin, the above method will not work.

In Spring Boot, we often need to read some predefined values ​​from application.properties, as follows:

# application.properties
my.food=pizza

If your project is written in Java, you can use @Value(“${name}”) to get the value of my.food.

@Value("${my.food}")
private String myFood;

However, if it is written in Kotllin, the above method will not work. You need to use @Value(“\${name}”) instead, and the variable must be declared as lateinit var.

@Value("\${my.food}")
private lateinit var myFood: String
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like