My application uses @DefaultValue annotation with a SpEl expression as default value. I assumed SpringBoot resolves the expression, but SpringBoot doesn't.
Example:
@SpringBootApplication
@EnableConfigurationProperties(SampleApplication.SampleProperties.class)
public class SampleApplication {
@ConstructorBinding
@ConfigurationProperties("sample")
public static class SampleProperties {
public SampleProperties(String prop1, @DefaultValue("${sample.prop1}") String prop2) {
System.out.println("prop1=" + prop1);
System.out.println("prop2=" + prop2);
}
}
public static void main(String[] args) {
System.setProperty("sample.prop1", "Hello World");
SpringApplication.run(SampleApplication.class, args);
}
}
Actual output:
prop1=Hello World
prop2=${sample.prop1}
Expected output:
prop1=Hello World
prop2=Hello World
It would be nice, if SpEl expressions in the @DefaultValue annotations would be resolved as for other Spring annotations, for example, the@Value annotation.
Comment From: wilkinsona
Thanks for the suggestion.
The @DefaultValue annotation is intended to provide a default value for an immutable property, in the same way that you can provide a default value for a JavaBean-based property in the field declaration:
private String example = "defaultValue";
Property placeholders are intentionally not supported in either scenario. For simplicity we recommend that a property's default value is constant. This, among other things, allows it to be included in the configuration property metadata and displayed as part of auto-completion in your IDE.
Comment From: augusto-gtns
that make no sense for me since the application properties feature itself supports placeholders, and the @DefaultValue which belongs to properties.bind package and intend to represent a class driven application properties has not the same behaviour