Spring监听器之-----org.springframework.web.context.ContextLoaderListener
来源:互联网 发布:安装打印机没有usb端口 编辑:程序博客网 时间:2024/06/17 02:17
原文链接:
http://www.cnblogs.com/wuchaodzxx/p/6038895.html
在web.xml中配置
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext.xml的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
ApplicationContext.xml这个配置文件部一般默认放置在。applicationContext的默认的路径是”/WEB-INF/applicationContext.xml。也可以在web.xml中配置该文件的其他位置,配置如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
classpath:applicationContext-security.xml;
</param-value>
</context-param>
以下详解
org.springframework.web.context.ContextLoaderListener类实现了javax.servlet.ServletContextListener接口。ServletContextListener接口能够监听ServletContext对象的生命周期,因为每个web应用仅有一个ServletContext对象,故实际上该接口监听的是整个web应用。
实现该接口的类在web.xml中作为监听器配置后,当web应用启动后,会触发ServletContextEvent事件,调用ContextLoaderListener的contextInitialized(ServletContextEvent sce)方法。
ContextLoaderListener通过一个ContextLoader对象来初始化Spring容器。在contextInitialized方法中调用contextLoader.initWebApplicationContext(event.getServletContext())。
ContextLoader类的initWebApplicationContext方法即可返回一个WebApplicationContext对象context。并通过 servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context)将WebApplicationContext对象放置在ServletContext对象中。initWebApplicationContext方法通过调用以下方法实例化并设置WebApplicationContext对象。
protected WebApplicationContext createWebApplicationContext(ServletContext servletContext, ApplicationContext parent) throws BeansException { Class contextClass = determineContextClass(servletContext);//通过servletContext确定WebApplicationContext的具体类型 if(!(org.springframework.web.context.ConfigurableWebApplicationContext.class).isAssignableFrom(contextClass)) { throw new ApplicationContextException("Custom context class [" + contextClass.getName() + "] is not of type [" + (org.springframework.web.context.ConfigurableWebApplicationContext.class).getName() + "]"); } else { ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext)BeanUtils.instantiateClass(contextClass); wac.setParent(parent); wac.setServletContext(servletContext); wac.setConfigLocation(servletContext.getInitParameter("contextConfigLocation"));//设置配置文件的路径名 customizeContext(servletContext, wac); wac.refresh(); return wac; } }
因此可以通过WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)获取WebApplicationContext。内部实现是通过servletContext对象查找该对象,属性名为WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE。
- Spring监听器之-----org.springframework.web.context.ContextLoaderListener
- [Spring]error: org.springframework.web.context.ContextLoaderListener
- org.springframework.web.context.ContextLoaderListener
- org.springframework.web.context.ContextLoaderListener
- org.springframework.web.context.ContextLoaderListener
- org.springframework.web.context.ContextLoaderListener
- org.springframework.web.context.ContextLoaderListener
- org.springframework.web.context.ContextLoaderListener
- org.springframework.web.context.ContextLoaderListener .
- org.springframework.web.context.ContextLoaderListener
- org.springframework.web.context.ContextLoaderListener
- org.springframework.web.context.ContextLoaderListener
- org.springframework.web.context.ContextLoaderListener
- org.springframework.web.context.ContextLoaderListener
- org.springframework.web.context.ContextLoaderListener
- org.springframework.web.context.ContextLoaderListener
- org.springframework.web.context.ContextLoaderListener
- org.springframework.web.context.ContextLoaderListener
- mysql 命令行补全工具 mycli
- 我的Android工作笔记
- SSD对HADOOP集群性能提升 调研及测试计划
- 同时求交集和差集
- Android截屏方式
- Spring监听器之-----org.springframework.web.context.ContextLoaderListener
- Ubuntu下JDK配置和安装
- Android Studio:创建类时,添加作者名和日期等
- 卷积神经网络Step by Step(二)
- SDUT 3893 威佐夫+nim博弈(第八届山东ACM省赛A题)
- 文件权限的那些事儿(1)linux文件权限
- 工厂模式
- DNS原理及其解析过程
- 支付宝最新骗局,常用支付宝的需要注意了!