如何在spring的xmls中定义变量以在log4j.properties中使用

And*_*tov 15 java xml spring tomcat log4j

这里有点难题.

我在WAR中有一个应用程序.那里有web.xml和application context.xml,还有log4j.properties.此WAR在tomcat中运行.

有可能在log4j.properties中使用一些变量,例如 log4j.appender.file.File=${catalina.base}/logs/app.log

我想在web.xml或context.xml中定义一个变量,并在log4j.properties中使用它.例如,以某种方式设置version=1.1和使用log4j.appender.file.File=${catalina.base}/logs/app-${version}.log.它不应该是环境变量.

我可以不重新编译应用程序吗?

ADD 不应该影响任何事情,但以防万一......

它的web.xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5"
     xmlns="http://java.sun.com/xml/ns/javaee">

<!-- Spring -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:context.xml</param-value>
</context-param>

<!-- log4j -->
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
    <param-name>log4jRefreshInterval</param-name>
    <param-value>10000</param-value>
</context-param>
<context-param>
    <param-name>log4jExposeWebAppRoot</param-name>
    <param-value>false</param-value>
</context-param>
...
</web-app>
Run Code Online (Sandbox Code Playgroud)

Fox*_*BOA 9

创建一个org.springframework.web.util.Log4jConfigListener公开version值的子类作为System变量(当然,您可以通过标准的System/Java环境变量传递值).

public class TestListener extends Log4jConfigListener{

    @Override
    public void contextInitialized(ServletContextEvent pEvent) {
        String value = pEvent.getServletContext().getInitParameter("version");
        String version = System.getProperty("version");
        if (version == null || version.trim().length()==0) System.setProperty("version", value);

        super.contextInitialized(pEvent);
    }
}
Run Code Online (Sandbox Code Playgroud)

并修复你的 web.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee">

    <!-- Spring -->
    ...
    <!-- log4j -->
    <listener>
        <listener-class>com.TestListener</listener-class>
    </listener>
    <context-param>
        <param-name>version</param-name>
        <param-value>1.1</param-value>
    </context-param>
    ...
Run Code Online (Sandbox Code Playgroud)


jav*_*bot 0

我认为您可以通过在 web.xml 中声明上下文参数并在要放置版本或其他内容的属性文件中使用该参数名称来实现此目的。

在 web.xml 中

<context-param>
    <param-name>version</param-name>
    <param-value>1.1</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

在属性文件中

log4j.appender.file.File=${version}/logs/app.log
Run Code Online (Sandbox Code Playgroud)

这应该可以在服务器重新启动的情况下工作,无需编译。

您还可以尝试从 xml 设置属性并从属性文件访问 ti。

在 web.xml 中

<Properties>
    <Property name="version">1.1</Property>
</properties>
Run Code Online (Sandbox Code Playgroud)

在属性文件中

log4j.appender.file.File=${version}/logs/app.log
Run Code Online (Sandbox Code Playgroud)