1、IntrospectorCleanupListener
IntrospectorCleanupListener应该注册为web.xml中的第一个Listener,在任何其他Listener之前注册。 该监听器在web.xml中的配置如下:
<listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener>
在Web应用程序关闭时IntrospectorCleanupListener将会刷新JDK的JavaBeans的Introspector缓存。在你的web.xml中注册这个listener来确保Web应用程序的类加载器以及其加载的类正确的释放资源。
如果JavaBeans的Introspector已被用来分析应用程序类,系统级的Introspector缓存将持有这些类的一个硬引用。因此,这些类和Web应用程序的类加载器在Web应用程序关闭时将不会被垃圾收集器回收!而IntrospectorCleanupListener则会对其进行适当的清理,已使其能够被垃圾收集器回收。
2、Log4jConfigListener
Spring提供了一个Log4jConfigListener,本身就能通过web.xml中配置来指定位置加载log4j配置文件和log输出路径,但是该 listener需要放在spring的Listener之前。
使用spring中的Log4jConfigListener有如如下好处:
1、动态的改变日志级别和策略,不需要重启Web应用。
2、把log文件定在 /WEB-INF/logs/ 而不需要写绝对路径。
系统把web目录的路径压入一个叫webapp.root的系统变量,日志保存路径就可以用相对路径了
log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/monia.log
3、可以把log4j.properties和其他properties一起放在/WEB-INF/ ,而不是Class-Path。
4、log4jRefreshInterval为6000表示开一条watchdog线程每6秒扫描一下配置文件的变化。
在web.xml文件中添加如下配置:
<!-- 当同一个应用服务器中部署两个以上用到Log4jConfigListener的应用时,需要通过该参数加以区别 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>some.web.root</param-value> </context-param> <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>
3、WebAppRootListener
这个监听器就会在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的跟目录的物理路径了。
监听web.xml中的配置para-name为webAppRootKey的值,
<context-param> <param-name>webAppRootKey</param-name> <param-value>some.web.root</param-value> </context-param>
再配置该监听器
<listener> <listener-class>org.springframework.web.util.WebAppRootListener</listener-class> </listener>
如果在web.xml中已经配置了Log4jConfigListener监听器,则不需要配置WebAppRootListener了。因为Log4jConfigListener已经包含了WebAppRootListener的功能。
4、ContextLoaderListener
启动Web容器时,自动装配ApplicationContext的配置信息。该监听器在web.xml中的配置如下:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
如果在web.xml中不写任何参数配置信息,默认的路径是/WEB-INF/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml;如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/applicationContext-*.xml, claaspath:/applicationContext-*.xml </param-value> </context-param>
5、RequestContextListener
在Spring2.0中除了以前的Singleton和Prototype外又加入了三个新的web作用域,分别为request、session和global session。
Request作用域:
针对每次HTTP请求,Spring容器都会创建一个全新的bean实例,且该bean实例仅在当前HTTP request内有效。当处理请求结束,request作用域的bean实例将被销毁。
Session作用域:
针对某个HTTP Session,Spring容器会创建一个全新的bean实例,且该bean仅在当前HTTP Session内有效。当HTTP Session最终被废弃的时候,在该HTTP Session作用域内的bean也会被废弃掉。
global session作用域:
global session作用域类似于标准的HTTP Session作用域,不过它仅仅在基于portlet的web应用中才有意义。Portlet规范定义了全局Session的概念,它被所有构成某个portlet web应用的各种不同的portlet所共享。在global session作用域中定义的bean被限定于全局portlet Session的生命周期范围内。
作用域依赖问题:如果将Web相关作用域的Bean注入到singleton或prototype的Bean中,这种情况下,需要Spring AOP
<bean name="car" class="com.demo.Car" scope="request"> <aop:scoped-proxy/> </bean> <bean id="boss" class="com.demo.Boss" > <properrty name="car" ref="car" /> </bean>
如果想让容器里的某个bean拥有其中某种新的web作用域,除了在bean级上配置相应的scope属性,还必须在容器级做一个额外的初始化配置。该监听器在web.xml中的配置如下:
<listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>