我的 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());
}
}
关于spring - 如何在Spring项目中使用properties来配置log4j.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6127515/