一个tomcat加载两个项目,报错以下:html
严重: 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' = [/home/ghw/apache-tomcat-7.0.75/webapps/qazitem/] instead of [/home/ghw/apache-tomcat-7.0.75/webapps/qazapp/] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!java
webAppRootKey是在java web项目的web.xml配置文件中表示项目的惟一标示,在Eclipse调试Web项目时,项目的路径是一个临时路径,不在真正的路径下,能够经过log4j日志的方式打印出属性值,来看看临时项目路径在哪里,能够用System.getProperty("web.sample.root");若是web.xm 内没有设置webAppRootKey项,是为默认设置,那么webAppRootKey就是缺省的"webapp.root"。web
下面是相关源码spring
1. · public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException {apache
2. String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);tomcat
3. String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);app
4. String oldValue = System .getProperty(key);webapp
5. if (oldValue != null ) {spa
6. throw new IllegalStateException("WARNING: Web app root system property already set: " + key + " = " +调试
7.
8.
9. oldValue + " - Choose unique webAppRootKey values in your web.xml files!" );
10. }
11. String root = servletContext.getRealPath("/" );
12. if (root == null ) {
13. throw new IllegalStateException("Cannot set web app root system property when WAR file is not
14.
15. expanded");
16. }
17. System .setProperty(key, root);
18. servletContext.log("Set web app root system property: " + key + " = " + root);
19. }
20.
21.
从代码看出,该方法其实就是把该web application的根目录的绝对文件路径做为属性保存在 System的属性列表中。该属性的名字,由web.xml文件中的名为"webAppRootKey"的参数值指出。若是不在web.xml中定义 webAppRootKey参数,那么属性名就是缺省的"webapp.root".
但最好设置,以避免项目之间的名称冲突。
Spring经过 org.springframework.web.util.WebAppRootListener 这个监听器来压入项目路径。可是若是在web.xml中已经配置了 org.springframework.web.util.Log4jConfigListener
这个监听器,则不须要配置WebAppRootListener了。由于Log4jConfigListener已经包含了WebAppRootListener的功能
部署在同一容器中的Web项目,要配置不一样的<param-value>,不能重复
若是配置了
log4j.appender.file.File=${web.sample.root}/WEB-INF/logs/sample.log
log4j会本身自动创建logs目录, 不须要手工显式创建空的logs目录
解决方案:
在启动出现错误的工程web.xml增长以下语句即可
<context-param>
<param-name>webAppRootKey</param-name>
<param-value> app.root </param-value>
</context-param>