一、问题描述

    最近公司有了一个新项目,这个项目最近部署到测试服务器上的时候出现了一个问题。

严重: 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' = [ ] instead of [ ] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files! 

在eclipse下面启动是没问题的,打包的时候也没有报错。唯一的区别是测试服务器上tomcat里起了多个项目。

二、解决方法

从网上找了各种资料,解决方法是在web.xml中加一段代码

<context-param>
<param-name>webAppRootKey</param-name>
<param-value>projectName.root</param-value> <!-- 建议用"工程名字.root"-->
</context-param>

加了之后,果然解决了。

三、具体原因

    我是那种不找到具体原因就不甘心的人。

首先,这个项目用到了log4j(貌似现在大部分项目都在用)。log4j启动时,默认会寻找source folder(src)下的log4j.xml配置文件,若没有,会寻找log4j.properties文件。如果log4j放到别的位置,则需要在web.xml中配置。通过log4jConfigLocation指定文件的位置。

  <context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:config/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>6000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

问题来了,问题就出在这个org.springframework.web.util.Log4jConfigListener类里面,点进去看这个class。

public class Log4jConfigListener implements ServletContextListener {

    @Override
public void contextInitialized(ServletContextEvent event) {
Log4jWebConfigurer.initLogging(event.getServletContext());
} @Override
public void contextDestroyed(ServletContextEvent event) {
Log4jWebConfigurer.shutdownLogging(event.getServletContext());
} }

再点进去initLogging这个方法。

public static void initLogging(ServletContext servletContext) {
// Expose the web app root system property.
if (exposeWebAppRoot(servletContext)) {
WebUtils.setWebAppRootSystemProperty(servletContext);
} // Only perform custom log4j initialization in case of a config file.
String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
if (location != null) {
// Perform actual log4j initialization; else rely on log4j's default initialization.
try {
// Resolve property placeholders before potentially resolving a real path.
location = ServletContextPropertyUtils.resolvePlaceholders(location, servletContext); // Leave a URL (e.g. "classpath:" or "file:") as-is.........
      .........

再点进去setWebAppRootSystemProperty这个方法。

public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException {
Assert.notNull(servletContext, "ServletContext must not be null");
String root = servletContext.getRealPath("/");
if (root == null) {
throw new IllegalStateException(
"Cannot set web app root system property when WAR file is not expanded");
}
String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);
String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);
String oldValue = System.getProperty(key);
if (oldValue != null && !StringUtils.pathEquals(oldValue, root)) {
throw new IllegalStateException(
"Web app root system property already set to different value: '" +
key + "' = [" + oldValue + "] instead of [" + root + "] - " +
"Choose unique values for the 'webAppRootKey' context-param in your web.xml files!");
}
System.setProperty(key, root);
servletContext.log("Set web app root system property: '" + key + "' = [" + root + "]");
}

在这个类里面就能看到拋异常的语句以及拋异常之前处理的逻辑了。

如果两个工程都不配webAppRootKey,第一个工程启动运行到这个ok,但是第二个工程启动的时候运行到这里,if那里就报错了。

其实归根究底不是log4j的原因,只要涉及到这个方法,并且没有设置webAppRootKey都会报错。当启动多个工程的时候,应该要配置一下这个webAppRootKey。

IllegalStateException : Web app root system property already set to different value问题详解的更多相关文章

  1. java.lang.IllegalStateException:Web app root system property already set to different value 错误原因及解决 Log4j

    Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口 服务器.NT的事件记录器.UNIX Syslog守护进程等: ...

  2. 解决tomcat部署多个虚拟机时报IllegalStateException: Web app root system property already set to 的问题

    解决tomcat部署多个虚拟机时报IllegalStateException: Web app root system property already set to 的问题 在web.xml中添加如 ...

  3. java.lang.IllegalStateException: Web app root system property already set to different value

    webAppRootKey是在java web项目的web.xml配置文件中表示项目的唯一标示,在Eclipse调试Web项目时,项目的路径是一个临时路径,不在真正的路径下,可以通过log4j日志的方 ...

  4. Web app root system property already set to different value 错误原因及解决

    http://yzxqml.iteye.com/blog/1761540 ——————————————————————————————————————————————————————————————— ...

  5. Web app root system property already set to different value: 'webapp.root'

    java.lang.IllegalStateException: Web app root system property already set to different value: 'webap ...

  6. Cannot set web app root system property when WAR file is not expanded

    Cannot set web app root system property when WAR file is not expanded 在tomcat下面可以,在weblogic下面不行的处理方法 ...

  7. Web Api 接口返回值不困惑:返回值类型详解

    前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.之前分享过一篇 WebApi 接口参数:传参详解,这篇博文内容本身很基础 ...

  8. 安装mysql8.0.11及修改root密码、连接navicat for mysql的思路详解

    1.1. 下载: 官网下载zip包,我下载的是64位的: 下载地址:https://dev.mysql.com/downloads/mysql/ 下载zip的包: 下载后解压:(解压在哪个盘都可以的) ...

  9. html 5 本地数据库(Web Sql Database)核心方法openDatabase、transaction、executeSql 详解

    Web SQL数据库API实际上不是HTML5规范的组成部分,而是单独的规范.它通过一套API来操纵客户端的数据库.Safari.Chrome. Firefox.Opera等主流浏览器都已经支持Web ...

随机推荐

  1. 前端学习之回调函数、call方法、apply方法

    今天学习的内容比较少,大部分时间是自己在写qq音乐和京东移动端的页面.现在说说今天学到的内容: 首先,回调函数,就是在函数内部中调用另外一个函数, 将一个函数当作参数传给另一个函数,被传的函数叫做回调 ...

  2. proxyd.c

    /**************************************************************************** * program: proxyd * mo ...

  3. PeerConnection

    Example(摘) /*When two peers decide they are going to set up a connection to each other, they both go ...

  4. Vagrant 启用 rsync

    折腾了那么久,发现这些smb,nfs,virtualcfs,这些同步方案在windows下都不是最完美的.最完美的还是 rsync,我使用它同步windows上的代码,在windows浏览器中打开虚拟 ...

  5. PN结的单向导电性及PN结的电流方程及PN结电容

    PN结加正向电压 当PN结外加正向电压时,外电场将多数载流子推向空间电荷区,使其变窄,削弱了内电场,破坏了原来的平衡,使扩散运动加剧,PN结导通.PN结的压降只有零点几付,所以在其回路里应串联一个电阻 ...

  6. 关于IDW空间插值

    空间插值一般都会用到IInterPolationOP接口等 首先是通过图层的名称获取图层的方法: private ILayer GetLayerByName(string name)        { ...

  7. blogilo在chinaunix发布博客的设置

    1. 在日志类型菜单中选择"Metaweblog API". 2. 在日志的远程发布url中输入"http://blog.chinaunix.net/xmlrpc.php ...

  8. Python札记 -- 使用easy_install进行模块/包管理

    今天在阅读以前项目代码时,发现里面使用的第三方模块的参数相当诡异,总是对不上.经过分析之后,发现是自己安装的第三方模块跟项目使用的版本不一致.在Python中进行模块/包管理的话,就不得不提到easy ...

  9. SQL入门经典(三) 之连接查询

    上一篇介绍到查询.这一篇主要讲连接查询,将介绍INNER JOIN,OUTER JOIN(LEFT和RIGHT),FULL JOIN,CROSS JOIN. 连接顾名斯义就是把多个数据表数据合并到一个 ...

  10. [你必须知道的NOSQL系列]专题一:MongoDB快速入门

    一.前言 现在越来越多的公司开始采用非关系数据库了,并且很多公司的面试都要求面试者有MongoDB的使用经验,至于非关系数据库与关系型数据库之间的区别大家可以自行百度.但是作为程序员的我们,既然大部分 ...