Skip to content

Provide an alternative to @Value("\${foo}") for Kotlin projectsΒ #8061

Closed
@sdeleuze

Description

@sdeleuze

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.ioKotlin 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 ;-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions