web.xml配置文件中ContextLoaderListener的作用

来源:互联网 发布:桑拿软件 编辑:程序博客网 时间:2024/05/22 15:38

在搭建ssm框架的时候,在web.xml配置文件中配置监听器listener,它的作用是在启动web容器时,自动装配Spring的applicationContext.xml的配置信息。ContextLoaderListener继承ContextLoader类,所以加载applicationContext.xml的配置文件过程中由ContextLoader类来完成

<listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

那么,在配置applicationContext.xml的过程中,如果在web.xml中不写任何参数配置信息,默认的路径是/WEB-INF/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。

  1. 如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数
<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/classes/applicationContext-*.xml</param-value></context-param>

在param-value里指定相应的xml文件名,如果有多个xml文件,可以使用“,”号分隔。如上面的applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-mybatis.xml,applicationContext-hessian.xml,applicationContext-logic.xml等文件,都会一同被载入。

  1. 如果applicationContext.xml文件没有在/WEB-INF/下,或文件名不一致,或存在多个Spring配置文件,在web.xml文件中根据下面代码修改
<context-param>    <param-name>contextConfigLocation</param-name>        <param-value>          classpath*:applicationContext-*.xml,         /WEB-INF/classes/applicationContext-*.xml     <param-value>   </context-param>  
阅读全文
0 0
原创粉丝点击