spring项目中监听器作用_第1页
spring项目中监听器作用_第2页
spring项目中监听器作用_第3页
spring项目中监听器作用_第4页
spring项目中监听器作用_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

spring项目中监听器作用 1 spring框架的启动入口 ContextLoaderListener2作用:在启动Web容器时,自动装配SpringapplicationContext.xml的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成 pring在web下的入口在配置文件web.xml的监听器中<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class></listener><context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:conf/spring/applicationContext.xml</param-value></context-param>上述是在web.xml中的配置信息。 /实现了接口ServletContextListener,也就是说他必须实现contextDestroyed, contextInitialized这两个方法publicclass ContextLoaderListener implements ServletContextListener privateContextLoader contextLoader; /* *Initialize the root web application context. */Spring框架由此启动, contextInitialized也就是监听器类的main入口函数 publicvoid contextInitialized(ServletContextEvent event) this.contextLoader = createContextLoader(); this.contextLoader.initWebApplicationContext(event.getServletContext(); /* * Createthe ContextLoader to use. Can be overridden in subclasses. * returnthe new ContextLoader */ protectedContextLoader createContextLoader() return new ContextLoader(); /* * Returnthe ContextLoader used by this listener. * returnthe current ContextLoader */ publicContextLoader getContextLoader() return this.contextLoader; /* * Closethe root web application context. */ publicvoid contextDestroyed(ServletContextEvent event) if (this.contextLoader != null) this.contextLoader.closeWebApplicationContext(event.getServletContext(); 总的来说这个入口非常简单,所有实现都隐藏在ContextLoader类里,我们在下一篇的内容中讨论ContextLoader,如果你不知道为什么这里是程序的入口,那么复习一下ServletContextListener 接口和监听器的相关知识吧 ServletContext被Servlet程序用来与 Web容器通信。例如写日志,转发请求。每一个 Web应用程序含有一个Context,被Web应用内的各个程序共享。因为Context可以用来保存资源并且共享,所以我所知道的 ServletContext的最大应用是Web缓存-把不经常更改的内容读入内存,所以服务器响应请求的时候就不需要进行慢速的磁盘I/O了。 ServletContextListener是ServletContext的监听者,如果 ServletContext发生变化,如服务器启动时 ServletContext被创建,服务器关闭时 ServletContext将要被销毁。 在JSP文件中,application是 ServletContext的实例,由JSP容器默认创建。Servlet中调用 getServletContext()方法得到 ServletContext的实例。我们使用缓存的思路大概是: 1.服务器启动时,ServletContextListener的contextInitialized()方法被调用,所以在里面创建好缓存。可以从文件中或者从数据库中读取取缓存内容生成类,用 ervletContext.setAttribute()方法将缓存类保存在ServletContext的实例中。 2.程序使用 ServletContext.getAttribute()读取缓存。如果是 JSP,使用a pplication.getAttribute()。如果是 Servlet,使用 getServletContext().getAttribute()。如果缓存发生变化(如访问计数),你可以同时更改缓存和文件/数据库。或者你等 变化积累到一定程序再保存,也可以在下一步保存。 3.服务器将要关闭时,ServletContextListener的 contextDestroyed()方法被调用,所以在里面保存缓存的更改。将更改后的缓存保存回文件或者数据库,更新原来的内容。 Java代码 import User; /my own classimport DatabaseManager; / my own class import javax.servlet.ServletContext; import javax.servlet.ServletContextListener; public class MyContextListener implements ServletContextListener private ServletContext context = null; public void contextInitialized(ServletContextEvent event) context = event.getServletContext(); User user = DatabaseManager.getUserById(1); context.setAttribute(user1, user); public void contextDestroyed(ServletContextEvent event) User user = (User)context.getAttribute(user1); DatabaseManager.updateUserData(user); this.context = null; import User; /my ownclassimport DatabaseManager; /my own classimportjavax.servlet.ServletContext;importjavax.servlet.ServletContextListener;public classMyContextListener implementsServletContextListener private ServletContext context = null; public voidcontextInitialized(ServletContextEvent event) context =event.getServletContext(); User user =DatabaseManager.getUserById(1); context.setAttribute(user1, user); public voidcontextDestroyed(ServletContextEvent event) User user =(User)context.getAttribute(user1); DatabaseManager.updateUserData(user); this.context = null; 布署 ServletContextListener 你实现(implem

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论