如何在 XML 中使用 Spring Java 属性默认值

How to use Spring Java properties default value in XML

我正在寻找如何在 XML 中使用 Java 默认属性值,而无需在应用程序 YML 中指定或任何其他内容。

这是我的 java 配置和默认值我想使用这个 URL 值直到从 YML 文件提供它。

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

当我尝试在 XML

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

投掷

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

您在 XML 中使用的占位符不包括在缺少时使用的默认值

可以在占位符

上使用 :default-value 后缀提供默认值
<property name="serverURL" value="${test.sample.serverurl:http://localhost}" />

示例因默认值中的 : 而变得复杂,更简单的可能是

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

可能存在重复 Is there a way to specify a default property value in Spring XML?. Here is a decent tutorial on properties in Spring