log4j和web.xml配置webAppRootKey 的问题java
1 在web.xml配置 web
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>web.sample.root</param-value>
</context-param>spring
能够用System.getProperty("web.sample.root")来获取属性值。在Eclipse调试Web项目时,项目的路径是一个临时路径,不在真正的路径下,能够经过上述语句打印出属性值,来看看临时项目路径在哪里apache
如:System.out.println("web.root:"+ System.getProperty("web.root"));tomcat
输出:web.root:D:\apache-tomcat-6.0.30\webapps\wangzun\app
二、Spring经过 org.springframework.web.util.WebAppRootListener 这个监听器来压入项目路径。可是若是在web.xml中已经配置了 org.springframework.web.util.Log4jConfigListener 这个监听器,则不须要配置WebAppRootListener了。由于Log4jConfigListener已经包含了WebAppRootListener的功能.webapp
配置WebAppRootListener:spa
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
三、部署在同一容器中的多个Web项目,要配置不一样的<param-value>,不能重复webAppRootKey的系统变量名调试
4.WebAppRootListener要在ApplicationContext的ContextLoaderListener以前,不然ApplicationContext的bean注入根目录值时会发生没法注入异常。日志
<!-- 项目根目录Listener -->
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
<!--Spring的ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
四、若是配置了
log4j.appender.file.File=${web.sample.root}WEB-INF/logs/sample.log
log4j会本身自动创建logs目录, 不须要手工显式创建空的logs目录
在tomcat下部署两个或多个项目时,web.xml文件中最好定义webAppRootKey参数,若是不定义,将会缺省为“webapp.root”,以下:
<!-- 应用路径 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>
最好报纸每一个项目的参数值不一样,以避免引发项目冲突
严重: Exception sending context initialized event to listener instance of class org.springframework.web.util.Log4jConfigListener
java.lang.IllegalStateException: Web app root system property already set to different value: 'webapp.root' = [C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\webapps\DRMProject\] instead of [C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\webapps\DRMSn\] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!
对多个项目要对webAppRootKey进行配置,这里主要是让log能将日志写到对应项目根目录下,如我配置这两个项目的webAppRootKey为
项目1 的 web.xml:
<!-- 应用路径 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root1</param-value>
</context-param>
<!-- 项目根目录Listener -->
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
项目2的 web.xml:
<!-- 应用路径 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root2</param-value>
</context-param>
<!-- 项目根目录Listener -->
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
这样就不会出现冲突了。
定义之后,在Web Container启动时将把ROOT的绝对路径写到系统变量里。
而后log4j的配置文件里就能够用${webName.root }来表示Web目录的绝对路径,把log文件存放于webapp中。
namemax:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>web.root</param-value>
</context-param>
<!-- 项目根目录Listener -->
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
bean中可使用:
<bean id="transformChinese" class="com.zunmi.util.TransformChinese"
p:outBasePath="${web.root}WEB-INF/destineDomainFile/"
p:j2fSource="${web.root}WEB-INF/SimpleToTraditional.properties" p:charSet="gbk" />