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

Moh*_*nam 2 java spring properties configurationproperty spring-boot

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

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

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

当我尝试在 XML 中使用时

<property name="serverURL" value="${test.sample.serverurl}" />
Run Code Online (Sandbox Code Playgroud)

投掷

IllegalArgumentException : Could not resolve placeholder 'test.sample.serverurl' in value "${test.sample.serverurl}"
Run Code Online (Sandbox Code Playgroud)

pto*_*mli 5

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

默认值可以通过:default-value占位符上的后缀提供

<property name="serverURL" value="${test.sample.serverurl:http://localhost}" />
Run Code Online (Sandbox Code Playgroud)

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

value="example:default"
value="test.sample.port:8080"
Run Code Online (Sandbox Code Playgroud)

有可能重复是否有办法在 Spring XML 中指定默认属性值?. 这是一个关于 Spring 属性不错的教程