Spring上下文ContextLoaderListener作用
来源:互联网 发布:淘宝助理密码错误 编辑:程序博客网 时间:2024/06/15 12:02
1·ContextLoaderListener 用法
ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启 动容器时,就会默认执行它实现的方法。
至于ApplicationContext.xml这个配置文件部署在哪,如何配置多个xml文件,书上都没怎么详 细说明。现在的方法就是查看它的API文档。在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置 过程由ContextLoader来完成。看看它的API说明。
第一段说明ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。如果查看ContextLoaderServlet的 API,可以看到它也关联了ContextLoader这个类而且它实现了HttpServlet这个接口。
第二段,ContextLoader创建的是 XmlWebApplicationContext这样一个类,它实现的接口是 WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->BeanFactory 这样一来spring中的所有bean都由这个类来创建
第三段,讲如何部署applicationContext的xml文件。
如果在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 </param-value> </context-param>
在 里指定相应的xml文件名,如果有多个xml文件,可以写在一起并一“,”号分隔。上面的 applicationContext-*.xml采用通配符,
比如这那个目录下有applicationContext-ibatis- base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml 等文件,都会一同被载入。
由此可见applicationContext.xml的文件位置就可以有两种默认实现:
第一种:直接将之放到/WEB-INF下,之在web.xml中声明一个listener;
第二种:将之放到classpath下,但是此时要在web.xml中加入,用它来指明你的 applicationContext.xml的位置以供web容器来加载。按照Struts2 整合spring的官方给出的档案,写成:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> </context-param>
2·initWebApplicationContext作用
ApplicationContext是Spring的核心,Context我们通常解释为上下文环境,我想用“容器”来表述它更容易理解一些,ApplicationContext则是“应用的容器”了;在Web应用中,
我们会用到WebApplicationContext,WebApplicationContext继承自ApplicationContext;WebApplicationContext的初始化方式和BeanFactory.ApplicationContext有所区别,因为WebApplicationContext需要ServletContext实例,
也就是说它必须拥有Web容器的前提下才能完成启动的工作.有过Web开发经验的读者都知道可以在web.xml中配置自启动的Servlet或定义Web容器监听器(ServletContextListener),借助着两者中的任何一个,我们就可以启动Spring Web应用上下文的工作.
Spring分别提供了用于启动WebApplicationContext的Servlet和Web容器监听器:
org.springframework.web.context.ContextLoaderServlet;
org.springframework.web.context.ContextLoaderListener.
这两个方法都是在web应用启动的时候来初始化WebApplicationContext,我个人认为Listerner要比Servlet更好一些,因为Listerner监听应用的启动和结束,而Servlet得启动要稍微延迟一些,如果在这时要做一些业务的操作,启动的前后顺序是有影响的。
配置例子如下:
context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
那么在ContextLoaderListener和ContextLoaderServlet中到底做了什么呢?
以ContextLoaderListener为例,我们可以看到
public void contextInitialized(ServletContextEvent event) { this.contextLoader = createContextLoader(); this.contextLoader.initWebApplicationContext(event.getServletContext()); } protected ContextLoader createContextLoader() { return new ContextLoader(); }
ContextLoader是一个工具类,用来初始化WebApplicationContext,其主要方法就是initWebApplicationContext,我们继续追踪initWebApplicationContext这个方法(具体代码我不贴出,大家可以看Spring中的源码),
我们发现,原来ContextLoader是把WebApplicationContext(XmlWebApplicationContext是默认实现类)放在了ServletContext中,ServletContext也是一个“容器”,也是一个类似Map的结构,
而WebApplicationContext在ServletContext中的KEY就是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,我们如果要使用WebApplicationContext则需要从ServletContext取出,
Spring提供了一个WebApplicationContextUtils类,可以方便的取出WebApplicationContext,只要把ServletContext传入就可以了。
ApplicationContext 我们一般都是干这个用的
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); ProductMng productMng = (ProductMng)ctx.getBean("productMng");
- Spring上下文ContextLoaderListener作用
- Spring上下文ContextLoaderListener作用
- Spring上下文ContextLoaderListener作用
- Spring中的ContextLoaderListener作用
- Spring中的ContextLoaderListener作用
- Spring中的ContextLoaderListener作用
- Spring中的ContextLoaderListener作用
- Spring中的ContextLoaderListener作用
- spring ContextLoaderListener的作用
- [Spring] ContextLoaderListener的作用
- spring ContextLoaderListener的作用
- spring ContextLoaderListener的作用
- Spring中的ContextLoaderListener作用
- spring ContextLoaderListener的作用
- Spring:ContextLoaderListener作用
- Spring之ContextLoaderListener的作用
- Spring之ContextLoaderListener的作用
- Spring :监听器ContextLoaderListener的作用
- 鼠标 屏幕等事件
- 物联网网关使用中国移动物联卡
- 数据结构与算法JavaScript - 队列
- FragmentPagerAdapter实现分页
- pip install 卡住不动的解决方案
- Spring上下文ContextLoaderListener作用
- Java对象的深复制和浅复制
- UVA 796 Critical Links (桥)
- CodeForces 288C
- 基于JIRE做的一个项目度量平台
- Mongodb 3节点 Rep set +sharding 配置
- DataSetToJSON
- 专访程炳皓:八年一觉“开心”梦
- flex布局详解