一.Spring中的WebAppRootListener
這個listner的作用就是監聽web.xml中的配置para-name為webAppRootKey的值,比如我的web應用為tsts,那么我配置
這樣一個
1.<context-param>
2. <param-name>webAppRootKey</param-name>
3. <param-value>tsts.root</param-value>
4. </context-param>
5.,然后再配置這樣一個監聽器:
1.<listener>
2. <listener-class>
3. org.springframework.web.util.WebAppRootListener
4. </listener-class>
5. </listener>
。這個監聽器就會在web上下文初始化的時候,調用webUtil的對應方法,首先獲取到param-name對應的param-value ,然后,根據傳遞進去的ServletContext對象得到web的物理路徑:String root = servletContext.getRealPath("/");
接着把這個param-value作為key,root作為value放到system中System.setProperty(key, root);
然后再web中可以用 System.get.....就可以得到web的跟目錄的物理路徑了。
之前我的做法是用一個filter,在filter的init中調用String root = servletContext.getRealPath("/");,然后再去設置對應一個常量類文件的屬性。做法差不多,但是spring的做法更可以借鑒!
------------------------------------------
二.spring中的Log4jConfigListener
使用spring中的Log4jConfigListener有如如下好處:
1. 動態的改變記錄級別和策略,不需要重啟Web應用,如《Effective Enterprise Java》所說。
2. 把log文件定在 /WEB-INF/logs/ 而不需要寫絕對路徑。
因為 系統把web目錄的路徑壓入一個叫webapp.root的系統變量。這樣寫log文件路徑時不用寫絕對路徑了.
log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/myfuse.log
3. 可以把log4j.properties和其他properties一起放在/WEB-INF/ ,而不是Class-Path。
4.log4jRefreshInterval為60000表示 開一條watchdog線程每60秒掃描一下配置文件的變化;
在web.xml 添加
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
-----------------------------------
三.ContextLoaderListener
* Bootstrap listener to start up Spring's root WebApplicationContext.
* Simply delegates to ContextLoader.
* <p>This listener should be registered after Log4jConfigListener in web.xml,
* if the latter is used.
與BeanFactory
通常以編程的方式被創建不同的是,ApplicationContext
能以聲明的方式創建,如使用ContextLoader
。當然你也可以使用ApplicationContext
的實現之一來以編程的方式創建ApplicationContext
實例。首先,讓我們先分析ContextLoader
接口及其實現。
ContextLoader
機制有兩種方式,ContextLoaderListener
和ContextLoaderServlet
,他們功能相同但是listener不能在Servlet2.3容器下使用。Servlet2.4規范中servlet context listeners需要在web應用啟動並能處理初始請求時立即運行。(servlet context listener關閉的時候也是相同的)。servlet context listener是初始化SpringApplicationContext
理想的方式。你可能願意選擇ContextLoaderListener
,雖然是一樣的,但決定權在於你。你可以查看ContextLoaderServlet
的Javadoc來獲得更詳細的信息。
可以象下面所示例的一樣使用ContextLoaderListener
注冊一個ApplicationContext
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- or use the ContextLoaderServlet
instead of the above listener
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
-->
監聽器首先檢查contextConfigLocation
參數,如果它不存在,它將使用/WEB-INF/applicationContext.xml
作為默認值。如果已存在,它將使用分隔符(逗號、冒號或空格)將字符串分解成應用上下文件位置路徑。可以支持ant-風格的路徑模式,如/WEB-INF/*Context.xml
(WEB-INF文件夾下所有以"Context.xml"結尾的文件)。或者/WEB-INF/**/*Context.xml
(WEB-INF文件夾及子文件夾下的以"Context.xml"結尾的文件)。
ContextLoaderServlet
同ContextLoaderListener
一樣使用'contextConfigLocation'
參數。
-----------------------------------
四.ServletContextListener
------------------------------------
org.springframework.web.util.IntrospectorCleanupListener監聽器主要負責處理由JavaBean Introspector使用而引起的緩沖泄露,
它是一個在web應用關閉時清除JavaBean Introspector的監聽器,在web.xml中注冊這個listener可以保證在web應用關閉的時候釋放掉與這個web應用相關的class loader和由它管理的類.
如果你使用了JavaBeans Introspector來分析應用中的類.Introspector緩沖中會保留這些類的引用.結果在你的應用關閉的時候,這些類以及web應用相關的class loader沒有被垃圾回收.
不幸的是,清除Introsepctor的唯一方法是刷新整個緩沖,這是因為我們沒有辦法判斷哪些是屬於你的應用的引用,所以刪除被緩沖的Introspection會導致把這台電腦上的所有應用的introspection都刪掉.
spring托管的bean不需要使用這個監聽器.因為spring它自己的introspection所使用的緩沖在分析完一個類之后會被馬上從javabeans introspector緩沖中清除掉.
應用程序中的類從來不直接使用JavaBeans Introspector,所以他們一般不會導致內部查看資源泄露,但是一些類庫和框架往往會產生這個問題.如:Struts和Quartz.單個的內部查看泄露會導致整個的web應用的類加載器不能進行垃圾回收,在web應用關閉之后,你會看到此應用的所在靜態資源如單例,這個錯誤不是由該類自身引起的.
其他參考資料:
1.spring優秀工具類盤點 http://shijian0306.javaeye.com/blog/166036