-
Notifications
You must be signed in to change notification settings - Fork 40.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide an alternative to @Value("\${foo}") for Kotlin projects #8061
Comments
@Value("\${foo}")
for Kotlin projects
We've moved almost full gears to I'd be tempted to leave things as they are. |
I don't think it matters if we advise using it or not. If it doesn't work in Kotlin we should try and support it, and the suggestion sounds pragmatic. In Groovy you can just use '${foo}'. Is there no equivalent in Kotlin (a String literal with no replacements)? |
In Kotlin you have to escape the |
Ugliness is in the eye of the beholder, I suppose. It doesn't seem so bad to me, and it's a Spring annotation, so Spring dictates the contents, and if that means escaping, that's just the way it is. I guess I agree with @snicoll after all (it's fine the way it is). |
I understand, I guess we can just promote |
I have updated the related Stack Overflow question accordingly. An eventual improvement could be to support |
For what it's worth, here is my opinion: I use injection through constructor whenever possible and I also prefer to have my properties immutable, so I use |
@eacasanovaspedre We're not really happy with the mutable nature of |
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.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 forVoid
in Java).Another way to tackle this issue could be to allow customizing such prefix with an
application.properties
property + configuring that by default forstart.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 ;-)The text was updated successfully, but these errors were encountered: