log4j和web.xml配置webAppRootKey 的问题

在tomcat下部署两个或多个项目时,web.xml文件中最好定义webAppRootKey参数,如果不定义,将会缺省为“webapp.root”,如下:

  1. <!-- 应用路径  -->  
  2.     <context-param>  
  3.         <param-name>webAppRootKey</param-name>  
  4.         <param-value>webapp.root</param-value>  
  5.     </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!


可以用System.getProperty("webName.root")来获取属性值。在myEclipse调试Web项目时,项目的路径是一个临时路径,不在真正的路径下,可以通过上述语句打印出属性值,来看看临时项目路径在哪里

 

对多个项目要对webAppRootKey进行配置,这里主要是让log能将日志写到对应项目根目录下,如我配置这两个项目的webAppRootKey为

Xml代码
  1. <!-- 应用路径  -->  
  2.     <context-param>  
  3.         <param-name>webAppRootKey</param-name>  
  4.         <param-value>p2p_loan.root</param-value>  
  5.     </context-param>  
[xml]  view plain copy
  1. <!-- 应用路径  -->  
  2.     <context-param>  
  3.         <param-name>webAppRootKey</param-name>  
  4.         <param-value>webapp.root</param-value>  
  5.     </context-param>  

 

这样就不会出现冲突了。


定义以后,在Web Container启动时将把ROOT的绝对路径写到系统变量里。
然后log4j的配置文件里就可以用${webName.root }来表示Web目录的绝对路径,把log文件存放于webapp中。




Spring通过 org.springframework.web.util.WebAppRootListener 这个监听器来压入项目路径。但是如果在web.xml中已经配置了 org.springframework.web.util.Log4jConfigListener 这个监听器,则不需要配置WebAppRootListener了。因为Log4jConfigListener已经包含了WebAppRootListener的功能.

配置WebAppRootListener:

 <listener>
  <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
 </listener>


部署在同一容器中的多个Web项目,要配置不同的<param-value>,不能重复webAppRootKey的系统变量名


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目录



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>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值