logback + spring的webRootKey 多项目同时部署冲突问题

一次部署项目后出现下面的异常: 
Java代码    收藏代码
  1. java.lang.IllegalStateException: Web app root system property already set to different value: 'webapp.root' = [/usr/local/webserver/tomcat/webapps/naruto-manager/] instead of [/usr/local/webserver/tomcat/webapps/ysxj-manager/] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!  

网上很多都说是跟日志有关 
打开logback/log4j相关的spring监听类,比如LogbackConfigListener,初始化调用WebLogbackConfigurer的initLogging方法: 
Java代码    收藏代码
  1.  public static void initLogging(ServletContext servletContext) {  
  2.         // Expose the web app root system property.  
  3.         if (exposeWebAppRoot(servletContext)) {  
  4.             WebUtils.setWebAppRootSystemProperty(servletContext);  
  5.         }  
  6. ...  
  7. }  

追踪到WebUtils这个类中,打开这个类140行查看setWebAppRootSystemProperty方法,如下 
Java代码    收藏代码
  1. public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException {  
  2.         Assert.notNull(servletContext, "ServletContext must not be null");  
  3.         String root = servletContext.getRealPath("/");  
  4.         if (root == null) {  
  5.             throw new IllegalStateException(  
  6.                 "Cannot set web app root system property when WAR file is not expanded");  
  7.         }  
  8.         String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);  
  9.         String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);  
  10.         String oldValue = System.getProperty(key);  
  11.         if (oldValue != null && !StringUtils.pathEquals(oldValue, root)) {  
  12.             throw new IllegalStateException(  
  13.                 "Web app root system property already set to different value: '" +  
  14.                 key + "' = [" + oldValue + "] instead of [" + root + "] - " +  
  15.                 "Choose unique values for the 'webAppRootKey' context-param in your web.xml files!");  
  16.         }  
  17.         System.setProperty(key, root);  
  18.         servletContext.log("Set web app root system property: '" + key + "' = [" + root + "]");  
  19.     }  

原因很明显了: 
Web 容器启动的时候会获取webAppRootKey参数的值,然后以此值为可以key 把ROOT的绝对路径写到系统变量里, 
如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的"webapp.root",但如果多个项目都使用默认或相同的参数这里就会报错了 
解决方法: 
在web.xml中添加如下配置 
Xml代码    收藏代码
  1. <context-param>    
  2.     <param-name>webAppRootKey</param-name>  <!--如果放两个项目实例此属性要设置,而且两个项目param-value值的不能相同,log4j和logback都要设置,这点很容易忽略 -->  
  3.     <param-value>ysxj.root</param-value>    
  4. </context-param>  

这样在logback/log4j的配置文件中使用${ysxj.root }会用来表示Web目录的绝对路径
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值