web.xml配置webAppRootKey

web.xml文件中webAppRootKey屬性是web項目的絕對路徑,默認值是webApp.root,可以通過System.getProperty(“webApp.root”)來獲取屬性值或者在配置文件中通過${webApp.root}獲得。

<context-param>  
      <param-name >webAppRootKey</param-name > 
     <param-value >webApp.root </param-value > 
</context-param >

Spring通過 org.springframework.web.util.WebAppRootListener 這個監聽器來注入項目路徑,因此部署在同一個web容器中的項目,要配置不同的param-value(比如”項目名.root”),不然會造成衝突。但是如果在web.xml中已經配置了org.springframework.web.util.Log4jConfigListener這個監聽器,則不需要配置WebAppRootListener了。因爲Log4jConfigListener已經包含了WebAppRootListener的功能。WebAppRootListener要在ApplicationContext的ContextLoaderListener之前,否則ApplicationContext的bean注入根目錄值時會發生無法注入異常。
配置WebAppRootListener:

<listener>
        <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
 </listener>

web.xml中配置及其監聽器順序如下:

<!--注意各監聽器的順序,否則可能會出現上述的${webapp.root}/WEB-INF/log/sshtest.log不存在的異常-->  
    <!-- log4j config path -->  
  < context-param > 
    <param-name >log4jConfigLocation </param-name > 
    <param-value >/WEB-INF/classes/log4j.properties </param-value > 
  </ context-param > 
   <!-- webapp root path -->  
  < context-param > 
    <param-name >webAppRootKey </param-name > 
    <param-value >projectName.root </param-value > 
  </ context-param > 
      <!-- Spring相關的配置 -->
      <context-param >
            <param-name >contextConfigLocation </param-name >
            <param-value >/WEB-INF/applicationContext.xml,
           /WEB-INF/dataBeanContext.xml </param-value >
      </context-param >     

      <!-- log4j config listener -->  
      <listener > 
        <listener-class > 
            org.springframework.web.util.Log4jConfigListener 
        </listener-class > 
      </listener > 

      <!-- 使用ContextLoaderListener初始化Spring容器 -->
      <listener >
           <listener-class >org.springframework.web.context.ContextLoaderListener
            </listener-class >
      </listener >
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章