【问题标题】:Get value from application-lcl.properties in an xml configuration Spring从 xml 配置 Spring 中的 application-lcl.properties 获取值
【发布时间】:2020-09-15 12:51:27
【问题描述】:

我在一些 spring 应用程序中,在 application-lcl.properties 中有一行:

key1=value1

我想像这样在另一个 xml 中使用 key1 的值:

<appender name="ELASTIC" class="com.internetitem.logback.elasticsearch.ElasticsearchAppender">
    <url>${key1}</url>

${key1} 不起作用。你知道怎么做吗? (.xml 已经存在)

谢谢

【问题讨论】:

  • 您不能在 Spring 管理的 bean 之外使用 Spring 替换属性。在这种情况下,Spring 不会处理 logback.xml 文件进行属性替换。

标签: java spring spring-boot spring-mvc


【解决方案1】:

这是一个两步过程

  1. 将属性文件加载到 java.util.java.util.Properties 类对象中。

  2. 使用Properties.storeToXML()方法将内容写成XML

    字符串 inPropertiesFile = "application.properties"; 字符串 outXmlFile = "applicationProperties.xml";

    InputStream is = new FileInputStream(inPropertiesFile); //输入文件 OutputStream os = new FileOutputStream(outXmlFile); //输出文件

    属性 props = new Properties(); props.load(is);

    props.storeToXML(os, "application.properties","UTF-8");

【讨论】:

  • 嗨,xml 文件已经存在,我无法通过 InputStream 向其追加新行
【解决方案2】:

在xml中,放

&lt;springProperty name="value1" source="key1"/&gt;

然后通过调用来使用它

&lt;url&gt;${value1}&lt;/url&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2018-02-20
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    相关资源
    最近更新 更多