How to use Spring Java properties default value in XML

4.1k views Asked by At

I am looking how to use the Java default properties value in XML without specifying in application YML or what ever.

This is my java configuration and default I want to use this URL value until providing it from YML file.

@EnableConfigurationProperties
@ConfigurationProperties(prefix = "test.sample")
public @Data class SampleProperties {
   private String serverurl ="test.example.com";
}

When I try to use in XML

<property name="serverURL" value="${test.sample.serverurl}" />

Throwing

IllegalArgumentException : Could not resolve placeholder 'test.sample.serverurl' in value "${test.sample.serverurl}"
1

There are 1 answers

0
ptomli On BEST ANSWER

Your use of the placeholder in XML does not include a default value to use when it is missing

Default values can be provided with a :default-value suffix on the placeholder

<property name="serverURL" value="${test.sample.serverurl:http://localhost}" />

The example is complicated by the : in the default value, simpler ones might be

value="example:default"
value="test.sample.port:8080"

There is a probable duplicate Is there a way to specify a default property value in Spring XML?. Here is a decent tutorial on properties in Spring.