2

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?

Anthon
  • 69,918
  • 32
  • 186
  • 246
Vegard
  • 1,802
  • 2
  • 21
  • 34
  • Have you tried with Array instead of List? – Héctor Mar 15 '19 at 08:12
  • 1
    tried it now, got the same error: "java.lang.IllegalArgumentException: Could not resolve placeholder 'foo.bars' in value "${foo.bars}"" – Vegard Mar 15 '19 at 08:15
  • Have you defined the property value? It seems to me that the problem is that Spring can't find the property. – marstran Mar 15 '19 at 08:23
  • looks like the syntax I was using was wrong or not supported, I used what you see in the post, but it seems a comma separated list works out. – Vegard Mar 15 '19 at 08:28
  • There is work-around that works in Kotlin as well: https://stackoverflow.com/a/26700938/4395348 – Volodymyr Masliy Jun 19 '19 at 13:51

1 Answers1

2

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.

Volodymyr Masliy
  • 413
  • 4
  • 14