Using Sprint Boot 2.0.4.RELEASE, I have the following properties class (simplified):
@Configuration
@EnableConfigurationProperties(MyOtherHierarchicalNonConstantProperties.class)
public class AppConfiguration {
public static final String MY_CONSTANT = "${path.to.my-constant}";
}
and the application.yml
(also simplified):
path.to.my-constant: mystring
The Problem
The value from application.yml
never gets assigned to MY_CONSTANT
, which is required for use as an argument to annotations. (I want all configurable options between build environments to be set in application.yml
!) The value of MY_CONSTANT
ends up being ${path.to.my-constant}
.
I have been working with this issue for pretty much the entire day, also tried several different options after lots of searching, such as:
${${path.to.my-constant}}
#1#{${path.to.my-constant}}
#2${#{path.to.my-constant}}
#{'${path.to.my-constant}'}
- and many many more...
Additional (Maybe Helpful) Information
In some cases I get the exact expression as the value of the string (as said above), but in some cases I get exceptions such as:
- #1:
IllegalArgumentException
fromPropertyPlaceholderHelper.parseStringValue
sayingCould not resolve placeholder 'mystring' in value "${${path.to.my-constant}}"
- #2:
SpelEvaluationException
fromPropertyOrFieldReference.readProperty
sayingEL1008E: Property or field 'mystring' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?
Both indicating that if the initial expression is further wrapped inside other SpEL braces, the inner expression gets translated to the desired value. But after that, it refuses to be extracted from the outer braces. (For quite a long time, I was trying hundreds of SpEL operations to try and somehow "extract" the value from the outer braces...)
One thing I had also tried, is moving the constant into the other properties class with the @ConfigurationProperties
annotation hoping it would change something, but the results seem to be identical.
The Plea
I know the question about assigning configuration properties to constants in Spring Boot has been asked a lot, I have also tried every option on planet Earth after coming here to ask this and am hopelessly lost.
Someone please, for once and for all, put and end to this miserable lack of single source of truth about configurable constant expressions and tell us how to do this the right way! (And if Spring Boot never intended for it to be possible, that information could also be helpful to know...)
PS! No nasty hacks please...