Spring之ContextLoaderListener的作用
来源:互联网 发布:数学试卷制作软件 编辑:程序博客网 时间:2024/05/16 15:08
spring有三种启动方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn.
Spring org.springframework.web.context.ContextLoaderListener
public class ContextLoaderListener
作用:在启动Web容器时,自动装配SpringapplicationContext.xml的配置信息。
因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由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>
在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并一“,”号分隔。上面的applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入。
-----------------------------------------------------------------------------------------
例子,在web.xml文件中加入下面代码
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果applicationContext.xml文件没有在/WEB-INF/下,或文件名不一致,或存在多个Spring配置文件,在web.xml文件中根据下面代码修改
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml,/WEB-INF/applicationContext.xml,/WEB-INF/classes/applicationContext-*.xml
</param-value>
</context-param>
- 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的作用
- JavaMail学习笔记(一)、理解邮件传输协议(SMTP、POP3、IMAP、MIME)
- linux mknod命令
- 关于c语言的线程与linux驱动
- 索引
- C语言指针数组和数组指针
- Spring之ContextLoaderListener的作用
- gsoap使用心得!
- 函数
- Audio File Formats FAQ
- VS2010下内存泄露调试经历
- JavaMail学习笔记(二)、JavaMail API简介和配置开发环境
- Linux 常用命令
- 七夕在相爱人的眼中也是普通的一天 。
- ER图与范式