spring项目中监听器作用-ContextLoaderListener(转)
1 spring框架的启动入口 ContextLoaderListener
2 作用:在启动Web 容器时,自动装配Spring applicationContext.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 这两个方法
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());
}
}
}
总的来说这个入口非常简单 , 所有实现都隐藏在 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 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;
}
}
布署 ServletContextListener
你实现(implements) 了 ServletContextListener 编译后,把它放在正确的WEB-INF/classes 目录下,更改WEB-INF 目录下的 web.xml 文件,在web-app 节点里添加
<listener>
<listener-class>MyServletContextListener</listener-class>
</listener>
如果想更加深入了解,请阅读这篇文章 http://blog.csdn.net/ysughw/article/details/8992322
不让转载,发个链接
spring项目中监听器作用-ContextLoaderListener(转)的更多相关文章
- spring项目中监听器作用-ContextLoaderListener(项目启动时,加载一些东西到缓存中)
作用:在启动Web容器时,自动装配Spring applicationContext.xml的配置信息. 因为它实现了ServletContextListener这个接口,在web.xml配置这个监听 ...
- spring项目中监听器作用-ContextLoaderListener
附加链接:http://blog.csdn.net/zjw10wei321/article/details/40145241 作用:在启动Web 容器时,自动装配Spring applicationC ...
- SLAM+语音机器人DIY系列:(二)ROS入门——8.理解roslaunch在大型项目中的作用
摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...
- spring 项目中在类中注入静态字段
有时spring 项目中需要将配置文件的属性注入到类的静态字段中 例如:文件上传 //文件上传指定上传位置 //resource-dev.properties 有如下参数 #upload UPLOAD ...
- java web项目(spring项目)中集成webservice ,实现对外开放接口
什么是WebService?webService小示例 点此了解 下面进入正题: Javaweb项目(spring项目)中集成webservice ,实现对外开放接口步骤: 准备: 采用与spring ...
- spring项目中如何添加定时器以及在定时器中自动生成sprng注入对象
最近做了一个java的项目,部门领导给了一套代码让我尽快掌握,说心里话本人真心不喜欢java的这种项目方式,各种配置各种xml文件简直头都大了,下面就将我遇到的其中一个我认为是坑的地方整理出来,希望能 ...
- XML在JAVA项目中的作用
java项目中,xml文件一般都是用来存储一些配置信息 一般的编程, 多数用来存储配置信息 . 拿JDBC来说,可以把数据库连接字符串写到xml,如果要修改数据源,只需要改xml就可以了,没必要再去重 ...
- 9. spring项目中web.xml详解解读
引言:本篇博客的内容大部分都来自网上,有的是直接copy,有的是自己整理而来.既然网上已经有了,为啥还有自己copy呢? 感觉是因为网上的东西太散了或者是样式不够美观,所以自己又copy了一遍.如有侵 ...
- 【主流技术】ElasticSearch 在 Spring 项目中的实践
前言 ElasticSearch简称es,是一个开源的高扩展的分布式全文检索引擎. 它可以近乎实时的存储.检索数据,其扩展性很好,ElasticSearch是企业级应用中较为常见的技术. 下面和大家分 ...
随机推荐
- TP缓存设计方案解析
TP的缓存主要依赖Cache类,Cache类其实是一个代理类,Cache类通过getInstance静态方法来获取缓存实例,而getInstance方式实际是调用Cache类的connect方法,该方 ...
- Ajax and JSON
Ajax (核心是XMLHttpRequest对象) 1.XMLHttpRequest对象: request=new XMLHttpRequest() 支持Firefox opera Safari ...
- oracle新建表空间及用户
本文介绍命令模式(管理员权限): 1.以管理员权限打开命令控制台,输入下面命令: Sqlplus sys/管理员账户名称(就是DBA账户) as sysdba;(记得分号哦,有时没有的话会报错) 2. ...
- JS判断浏览器类型以及版本号
<script type="text/javascript"> (function(){ window.nav={}; ...
- 2016022613 - redis连接命令集合
redis连接命令 1.ping 用途:检查服务器是否正在运行 返回数据pong,表示服务器在运行. 2.quit 用途:关掉当前服务器连接 3.auth password 用途:服务器验证密码 没有 ...
- classpath目录
WEB-INF/ 是资源目录, 客户端不能直接访问, 这话是没错,不过现在的IDE编译器在编译时会把src下的文件(是文件,不是.java)移到WEB-INF/classes下.不过值得注意的是,sp ...
- [译]36 Days of Web Testing(六)
Day 30 Test in situ 真实场景下的测试 为什么? 我十分推崇现场测试,简单讲就是要在你的站点或应用真实使用的场景下进行测试.但随着人口增长,对于"真实场景"的定 ...
- Java实现希尔排序
华杰让我看了一道面试题:现有一段程序S,可以对任意n个数进行排序.如果现在需要对n^2个数进行排序,最少需要调用S多少次?(只允许调用S,不可以做别的操作). 看到了这 ...
- CentOS 6.0图解网络安装全过程
转自CentOS 6.0图解网络安装全过程 国内镜像站点(东北大学.网易) 网易镜像站点:http://mirrors.163.com/centos/6.0/isos/ 中科大镜像站点:http:// ...
- Feel Good
poj2796:http://poj.org/problem?id=2796 题意:给出一个长度为n(n<100000)的序列,求出一个子序列,使得这个序列中的最小值乘以这个序列的和的值最大. ...