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

2024-02-02

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

我的应用程序在 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());
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在Spring项目中使用属性来配置log4j.xml 的相关文章

随机推荐