Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Value with List in Kotlin from YAML

I am trying to initialise a list of properties from a YAML file in a Spring Boot project with Kotlin.

It works fine for a normal string, but fails when I try to init a List with the following error:

Unexpected exception during bean creation; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'foo.bars' in value "${foo.bars}"

The Kotlin code has the following constructor argument

@Value("\${foo.bars}")
val foobars: List<String>

the yaml file has the following values:

foo:
  bars:
  - test1
  - test2

Do I need to do something different between Lists and normal Strings?

like image 381
Vegard Avatar asked Dec 20 '25 21:12

Vegard


1 Answers

Solution described here can be changed a bit for Kotlin:

@Component
@ConfigurationProperties("foo")
class FooBarsProperties {
  lateinit var bars: List<String>
}

Simply inject FooBarsProperties where you need it. Don't forget to add @EnableConfigurationProperties to some of your configuration classes.

like image 100
Volodymyr Masliy Avatar answered Dec 23 '25 15:12

Volodymyr Masliy