SpringMVC中各种监听器的作用
2014-11-03 11:51
225 查看
1、ContextLoaderListener
作用:在启动Web容器时,自动装配Spring applicationContext.xml的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。
以下是web.xml中ContextLoaderListener的配置和context的配置。
context-param
// 实现了接口 ServletContextListener, 也就是说他必须实现 contextDestroyed, contextInitialized 这两个方法
<span style="font-size:18px;">public class ContextLoaderListener implements ServletContextListener { private ContextLoader contextLoader; /** * Initialize the root web application context. */ //Spring 框架由此启动 , contextInitialized 也就是监听器类的 main 入口函数 public void contextInitialized (ServletContextEvent event) { this.contextLoader = createContextLoader(); this.contextLoader.initWebApplicationContext(event.getServletContext()); } /** * Create the ContextLoader to use. Can be overridden in subclasses. * @return the new ContextLoader */ protected ContextLoader createContextLoader() { return new ContextLoader(); } /** * Return the ContextLoader used by this listener. * @return the current ContextLoader */ public ContextLoader getContextLoader() { return this.contextLoader; } /** * Close the root web application context. */ public void contextDestroyed (ServletContextEvent event) { if (this.contextLoader != null) { this.contextLoader.closeWebApplicationContext(event.getServletContext()); } } }</span>ServletContextListener 是 ServletContext 的监听者,如果 ServletContext 发生变化,如服务器启动时ServletContext 被创建,服务器关闭时 ServletContext 将要被销毁。
2、RequestContextListen
Spring2.0中新增了三个web作用域:request、session、global session,如果希望web容器中的某个bean具有新的作用域,除了在bean中配置相应的scope属性外,
还需要在容器中进行额外的初始化配置。
Request作用域
<bean id="loginAction" class="com.foo.LoginAction" scope="request"/>
针对每次HTTP请求,Spring容器会根据loginAction bean定义创建一个全新的LoginAction bean实例,且该loginAction bean实例仅在当前HTTP request内有效,因此可以根据需要放心的更改所建实例的内部状态,而其他请求中根据loginAction bean定义创建的实例,将不会看到这些特定于某个请求的状态变化。当处理请求结束,request作用域的bean实例将被销毁。
Session作用域
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
针对某个HTTP Session,Spring容器会根据userPreferences bean定义创建一个全新的userPreferences bean实例,且该userPreferences bean仅在当前HTTP Session内有效。与request作用域一样,你可以根据需要放心的更改所创建实例的内部状态,而别的HTTP Session中根据userPreferences创建的实例,将不会看到这些特定于某个HTTP Session的状态变化。当HTTP Session最终被废弃的时候,在该HTTP
Session作用域内的bean也会被废弃掉。
global session作用域
<bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/>
global session作用域类似于标准的HTTP Session作用域,不过它仅仅在基于portlet的web应用中才有意义。Portlet规范定义了全局Session的概念,它被所有构成某个portlet web应用的各种不同的portlet所共享。在global session作用域中定义的bean被限定于全局portlet Session的生命周期范围内。
请注意,假如你在编写一个标准的基于Servlet的web应用,并且定义了一个或多个具有global session作用域的bean,系统会使用标准的HTTP Session作用域,并且不会引起任何错误
为什么需要额外的配置RequestContextFilter
也许会有一个疑问,已经通过ContextLoaderListener(或ContextLoaderServlet)将Web容器与Spring容器整合,为什么这里还要用额外的RequestContextListener以支持Bean的另外3个作用域,原因是ContextLoaderListener实现ServletContextListener监听器接口,而ServletContextListener只负责监听Web容器的启动和关闭的事件。RequestContextFilter实现ServletRequestListener监听器接口,该监听器监听HTTP请求事件,Web服务器接收的每次请求都会通知该监听器。通过配置RequestContextFilter,Spring容器与Web容器结合的更加密切。
作用域依赖问题
如果将Web相关作用域的Bean注入到singleton或prototype的Bean中,这种情况下,需要Spring AOP
Java代码
<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>
3、IntrospectorCleanupListener
在服务器运行过程中,Spring不停的运行的计划任务和OpenSessionInViewFilter,使得Tomcat反复加载对象而产生框架并用时可能产生的内存泄漏,则使用IntrospectorCleanupListener作为相应的解决办法。
spring中的提供了一个名为org.springframework.web.util.IntrospectorCleanupListener的监听器。它主要负责处理由 JavaBeans Introspector的使用而引起的缓冲泄露。
spring中对它的描述如下:它是一个在web应用关闭的时候,清除JavaBeans Introspector的监听器.web.xml中注册这个listener.可以保证在web 应用关闭的时候释放与掉这个web 应用相关的class loader 和由它管理的类如果你使用了JavaBeans Introspector来分析应用中的类,Introspector 缓冲中会保留这些类的引用.结果在你的应用关闭的时候,这些类以及web
应用相关的class loader没有被垃圾回收.不幸的是,清除Introspector的唯一方式是刷新整个缓冲.这是因为我们没法判断哪些是属于你的应用的引用.所以删除被缓冲的introspection会导致把这台电脑上的所有应用的introspection都删掉.需要注意的是,spring 托管的bean不需要使用这个监听器.因为spring它自己的introspection所使用的缓冲在分析完一个类之后会被马上从javaBeans Introspector缓冲中清除掉.应用程序中的类从来不直接使用JavaBeans
Introspector.所以他们一般不会导致内部查看资源泄露.但是一些类库和框架往往会产生这个问题.例如:Struts 和Quartz.单个的内部查看泄漏会导致整个的web应用的类加载器不能进行垃圾回收.在web应用关闭之后,你会看到此应用的所有静态类资源(例如单例).这个错误当然不是由这个类自 身引起的.
引入很简单:
相关文章推荐
- SpringMVC中各种监听器的作用
- Spriingmvc中各种监听器的作用
- shell中各种括号的作用()、(())、[]、[[]]、{}
- 详解在Hibernate中配置数据库方言的作用和好处以及各种数据库的方言连接
- VC++中的各种文件作用的深入研究
- springMVC中web.xml中classpath的作用,及使用grandle构建项目
- springMVC之@RequestMapping作用
- springmvc常用注解和作用
- springmvc-5,HiddenHttpMethodFilter过滤器的作用
- springMVC各个包下的作用
- Android中的task作用和Activity启动模式以及各种启动Flag的含义
- Unity项目内各种文件的作用
- Android中Intent的各种常见作用。
- 安卓中各种用到的监听器
- "页面跳转"实例详解"SpringMVC注解"的作用与用法
- 详解ContextLoaderListener监听器的作用
- java通过给按钮添加监听器理解接口的作用
- 关于各种排序复杂度,哈夫曼树,路由器作用
- Linux下各种文件夹的作用
- android中各种img文件的作用以及系统启动过程