WebAppRootListener
這個listener的作用就是監聽web.xml中的配置param-name爲webAppRootKey的值:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>myroot</param-value>
</context-param>
然後配置一個然後配置一個監聽器:
<listener>
<listener-class>
org.springframework.web.util.WebAppRootListener
</listener-class>
</listener>
這個監聽器會在web上下文初始化的時候,cs調用webUtil的對應方法,首先獲取根傳遞進來的servletContext得到物理路徑,String path=servletContext.getRealPath("/"); 然後找到context-param的webAooRootKey對應的param-value,把param-value的值作爲key,上面配置的是"myroot", 接着執行System.setProperty("myroot",path)。這樣在web中就可以使用System.getProperty("myroot")來獲取系統的絕對路徑。
注:
1)如果只配置了監聽器,沒有配置webAppRootKey, 默認wenAppRootKey對應的param-value的值爲webapp.root。
2)上面得到系統路徑是Spring的做法,和平時自己採用的方法基本一致,都是寫一個監聽器,然後得到物理路徑後手動放入System中,一般還會在這個監聽器中加載配置文件,獲取配置文件的值。