一個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>