Description
As discussed in #5537 and this Stackoverflow question, Spring default property placeholder prefix is not a good fit with Kotlin because it is already used in the language for String interpolation and #{...}
can't be used as a droppin replacement and has other meaning.
The current workaround I suggest to Kotlin users is to declare these customized PropertySourcesPlaceholderConfigurer
that allow to support @Value("%{foo}")
without breaking existing Java @Value("${foo}")
or Kotlin @Value("\${foo}")
annotations.
@Bean
fun kotlinPropertyConfigurer() = PropertySourcesPlaceholderConfigurer().apply {
setPlaceholderPrefix("%{")
setIgnoreUnresolvablePlaceholders(true)
}
@Bean
fun defaultPropertyConfigurer() = PropertySourcesPlaceholderConfigurer()
I suggest @Value("%{foo}")
since that sounds a not so bad convention, but it could obviously be a different one.
It would be nice if that could be done by default (and documented) in Spring Boot 2.0, using for example @ConditionalOnClass(Unit.class)
(kotlin.Unit
is the Kotlin class equivalent for Void
in Java).
Another way to tackle this issue could be to allow customizing such prefix with an application.properties
property + configuring that by default for start.spring.io
Kotlin projects, but I tend to think providing such default convention for Kotlin projects is more what I would expect from Spring Boot, but that's open to discussion ;-)