加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在Spring项目中使用属性来配置log4j.xml

发布时间:2020-12-15 01:28:22 所属栏目:大数据 来源:网络整理
导读:我的Spring项目中有多个属性文件. spring上下文加载这些属性并以方便的方式处理属性覆盖.有没有办法获取我的Spring配置XML文件(即${myprop})可用的属性,并在我的log4j.xml文件中以类似的方式使用它们?我知道我可以在启动时使用-Dprop = value将系统属性传递

我的Spring项目中有多个属性文件. spring上下文加载这些属性并以方便的方式处理属性覆盖.有没有办法获取我的Spring配置XML文件(即${myprop})可用的属性,并在我的log4j.xml文件中以类似的方式使用它们?我知道我可以在启动时使用-Dprop = value将系统属性传递给log4j,但我更喜欢在项目的属性文件中使用所有配置.这可能吗?

我的应用程序在Tomcat中运行.

最佳答案
将多个属性文件集成到一个属性后,尝试使用此类.

public class DOMConfiguratorWithProperties extends DOMConfigurator {

    private Properties propertiesField = null;

    public synchronized Properties getProperties() {
        return propertiesField;
    }

    public synchronized void setProperties(final Properties properties) {
        propertiesField = properties;
    }

    @Override
    protected String subst(final String value) {
        return super.subst(value,getProperties());
    }

    public static void configure(final String filename) {
        new DOMConfiguratorWithProperties().doConfigure(
                filename,LogManager.getLoggerRepository());
    }

    public static void configure(
            final String filename,final Properties properties) {
        DOMConfiguratorWithProperties configurator = new DOMConfiguratorWithProperties();
        configurator.setProperties(properties);
        configurator.doConfigure(
                filename,LogManager.getLoggerRepository());
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读