log4j和web.xml配置webAppRootKey 的問題java
1 在web.xml配置 web
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>web.sample.root</param-value>
</context-param>spring
能夠用System.getProperty("web.sample.root")來獲取屬性值。在Eclipse調試Web項目時,項目的路徑是一個臨時路徑,不在真正的路徑下,能夠經過上述語句打印出屬性值,來看看臨時項目路徑在哪裏apache
如:System.out.println("web.root:"+ System.getProperty("web.root"));tomcat
輸出:web.root:D:\apache-tomcat-6.0.30\webapps\wangzun\app
二、Spring經過 org.springframework.web.util.WebAppRootListener 這個監聽器來壓入項目路徑。可是若是在web.xml中已經配置了 org.springframework.web.util.Log4jConfigListener 這個監聽器,則不須要配置WebAppRootListener了。由於Log4jConfigListener已經包含了WebAppRootListener的功能.webapp
配置WebAppRootListener:spa
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
三、部署在同一容器中的多個Web項目,要配置不一樣的<param-value>,不能重複webAppRootKey的系統變量名調試
4.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目錄
在tomcat下部署兩個或多個項目時,web.xml文件中最好定義webAppRootKey參數,若是不定義,將會缺省爲「webapp.root」,以下:
<!-- 應用路徑 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</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!
對多個項目要對webAppRootKey進行配置,這裏主要是讓log能將日誌寫到對應項目根目錄下,如我配置這兩個項目的webAppRootKey爲
項目1 的 web.xml:
<!-- 應用路徑 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root1</param-value>
</context-param>
<!-- 項目根目錄Listener -->
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
項目2的 web.xml:
<!-- 應用路徑 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root2</param-value>
</context-param>
<!-- 項目根目錄Listener -->
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
這樣就不會出現衝突了。
定義之後,在Web Container啓動時將把ROOT的絕對路徑寫到系統變量裏。
而後log4j的配置文件裏就能夠用${webName.root }來表示Web目錄的絕對路徑,把log文件存放於webapp中。
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>
bean中可使用:
<bean id="transformChinese" class="com.zunmi.util.TransformChinese"
p:outBasePath="${web.root}WEB-INF/destineDomainFile/"
p:j2fSource="${web.root}WEB-INF/SimpleToTraditional.properties" p:charSet="gbk" />