springMVC中contextLoaderListener作用
来源:互联网 发布:seo链轮插件 编辑:程序博客网 时间:2024/05/22 06:18
ContextLoaderListener
- 作用:
启动web容器时自动装配ApplicationContext的配置信息.
因为它实现了ServletContextListener这个接口,在web.xml中配置这个监听器,启动容器时,就会默认执行它实现的方法.至于ApplicationContext.xml这个配置文件部署在哪,如何配置多个xml文件,就是查看API文档.在ContextLoaderListener中关联了contextLoader这个类,所以整个加载配置过程有contextLoader来完成
-API说明
contextLoader可以由contextLoaderListener和contextLoaderServlet来生成.如果查看contextLoaderServlet的API,可以看到它也关联了ContextLoader这个类而且它实现了HttpServlet这个接口,以下是大纲:查看结构可以在eclipse中进入类中按下ctrl+O
2.contextLoader创建的是XmlWebApplicationContext这样一个类,它实现的接口时WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->BeanFactory这样一来Spring中的所有bean都由这个类来创建.
3.如何部署applicationContext的xml文件,如果在web.xml中不写任何参数配置信息,默认的路径是”/WEB-INF/applicationContext.xml”,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml.
如果你想自定义Context参数
可以在web.xml中加入contextConfigLocation这个context参数
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext-*.xml </param-value> </context-param>
以上是在WEB-INF的classes目录下创建的以applicationContext-XXX.xml命名的xml文件
你亦可以在src下创建xml文件,只需要更改为
你也可以写多个
<!-- Context Configuration locations for Spring XML files --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> </context-param>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-*.xml</param-value> </context-param>
- 由此可见
在``<param-value><param-value>
里指定相应的xml文件名,如果有多个xml文件,可以写在一起,并以”,”分隔. - 上面的applicationContext-*.xml采用了通配符,比如src下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入.
总结
applicationContext.xml的文件就可以有两个默认实现:
- 直接将所要自定义的context放到/WEB-INF下,再在web.xml中声明一个listener
- 将所要自定义的context放到classpath(src)下,但此时要在web.xml中加入,用它来指明你的applicationContext.xml的位置以供web容器来加载.按照struts2整合spring的官方文档写法:
<!-- Context Configuration locations for Spring XML files -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext- *.xml</param-value>
</context-param>
感兴趣的同学可以看看源码:contextLoader源码分析
喜欢的同学可以点个赞哦,你的支持是我最大的前进动力.
- springMVC中contextLoaderListener作用
- springmvc ContextLoaderListener作用
- spring中ContextLoaderListener的作用
- SpringMVC--ContextLoaderListener
- ContextLoaderListener作用
- spring项目中监听器作用-ContextLoaderListener
- spring项目中监听器作用-ContextLoaderListener
- 唯爱小粽子:Spring中ContextLoaderListener的作用
- web.xml配置文件中ContextLoaderListener的作用
- [Spring]web.xml中配置ContextLoaderListener监听器的作用
- [Spring]web.xml中配置ContextLoaderListener监听器的作用
- springMVC之ContextLoaderListener分析
- SpringMVC容器初始化 ------ContextLoaderListener
- SpringMVC ContextLoaderListener监听器功能分析
- SpringMVC容器初始化 ContextLoaderListener
- Spring中的ContextLoaderListener作用
- Spring中的ContextLoaderListener作用
- Spring中的ContextLoaderListener作用
- cpp11 的变量类型自动推断
- 割顶
- bootstrap视频学习笔记(2017年11月26日21:56:33)
- 1.1线程的随机性验证
- 根据python的不同职业方向确定学习内容
- springMVC中contextLoaderListener作用
- linux内核设计与实现[第2章 摘抄]
- 如何用webgl(three.js)搭建一个3D库房
- RM装甲检测程序分析(二)
- js普通事件和监听事件的区别
- C++ Primier读书笔记(第三章)
- 2017-11-26日总结
- CSS中的块级元素、行内元素和空元素
- Linux中总线、设备、驱动是如何关联的?