【问题标题】:How to use Spring Java properties default value in XML如何在 XML 中使用 Spring Java 属性默认值
【发布时间】:2018-03-21 17:31:05
【问题描述】:

我正在寻找如何在 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}"

【问题讨论】:

    标签: java spring spring-boot properties configurationproperty


    【解决方案1】:

    您在 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?。这是decent tutorial on properties in Spring

    【讨论】:

      猜你喜欢
      • 2021-01-21
      • 2015-09-16
      • 2012-06-30
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 2013-04-09
      • 1970-01-01
      • 2020-02-20
      相关资源
      最近更新 更多