
1, 在web.xml中定义 contextConfigLocation参数.spring会使用这个参数加载.所有逗号分割的xml.如果没有这个参数,spring默认加载web-inf/applicationContext.xml文件.
contextConfigLocation classpath*:conf/spring/applicationContext_core*.xml,classpath*:conf/spring/applicationContext_dict*.xml,classpath*:conf/spring/applicationContext_hibernate.xml,classpath*:conf/spring/applicationContext_staff*.xml,classpath*:conf/spring/applicationContext_security.xmlclasspath*:conf/spring/applicationContext_modules*.xmlclasspath*:conf/spring/applicationContext_cti*.xmlclasspath*:conf/spring/applicationContext_apm*.xml
contextConfigLocation 参数定义了要装入的 Spring 配置文件。原理说明如下:
1.利用ServletContextListener 实现。
Spring 提供ServletContextListener 的一个实现类ContextLoaderListener ,该类可以作
为listener 使用,它会在创建时自动查找WEB-INF/ 下的applicationContext.xrnl 文件。因
此,如果只有一个配置文件,并且文件名为applicationContext.xml ,则只需在web.xml
文件中增加如下代码即可:
org.springframework.web.context.ContextLoaderListener
如果有多个配置文件需要载入,则考虑使用
因此,配置context-param时参数名字应该是contextConfigLocation。
带多个配置文件的web.xml 文件如下:
<1-- XML 文件的文件头二〉< 1-- web.xm1 文件的DTD 等信息一〉<1-- 参数名为contextConfigLocation…〉 contextConfigLocation /WEB-工NF/daoContext.xml./WEB-INF/applicationContext.xmlorg.spr工ngframework.web.context.ContextLoaderListener
如果没有contextConfigLocation 指定配置文件,则Spring 自动查找application
Context. xrnl 配置文件。如果有contextConfigLocation,则利用该参数确定的配置文件。
该参数指定的一个字符串, Spring 的ContextLoaderListener 负责将该字符串分解成多个
配置文件,逗号","、空格" "及分号";"都可作为字符串的分割符。
如果既没有applicationContext. xrnl 文件,也没有使用contextConfigLocation参数确
定配置文件,或者contextConfigLocation确定的配置文件不存在。都将导致Spring 无法
加载配置文件或无法正常创建ApplicationContext 实例
配置一个spring为加载而设置的servlet可以达到同样效果.
采用load-on-startup Servlet 实现。
Spring 提供了一个特殊的Servllet 类: ContextLoaderServlet。该Servlet 在启动时,会
自动查找WEB-IN日下的applicationContext. xml 文件。
当然,为了让ContextLoaderServlet 随应用启动而启动,应将此Servlet 配置成
load-on-startup 的Servleto load-on-startup 的值小一点比较合适,因为要保证Application
Context 优先创建。如果只有一个配置文件,并且文件名为applicationContext. xml ,则在
web.xml 文件中增加如下代码即可:
context org.springframework.web.context.ContextLoaderServlet l
。带多个配置文件的web且nl 文件如下:
<'一确定多个配置文件一> contextConfigLocation 热门相关搜索北京印刷学院排名 北方工业大学排名 北京航空航天大学排名 首都经济贸易大学排名 中国传媒大学排名 首都师范大学排名 中国地质大学(北京)排名 北京信息科技大学排名 中央民族大学排名 中国戏曲学院排名 河北政法职业学院排名 河北经贸大学排名 天津中德应用技术大学排名 天津医学高等专科学校排名 天津美术学院排名 天津音乐学院排名 天津工业大学排名 北京工业大学耿丹学院排名 北京警察学院排名 天津科技大学排名 铁岭卫生职业学院排名 沈阳北软信息职业技术学院排名 吉林艺术学院排名 吉林体育学院排名 白城师范学院排名 通化师范学院排名 吉林农业大学排名 长春电子科技学院排名 吉林工业职业技术学院排名 吉林司法警官职业学院排名 吉林警察学院排名 长春健康职业学院排名 吉林水利电力职业学院排名 大庆师范学院排名 黑龙江大学排名 哈尔滨剑桥学院排名 哈尔滨铁道职业技术学院排名 哈尔滨应用职业技术学院排名 黑龙江民族职业学院排名 七台河职业学院排名 黑龙江生态工程职业学院排名 华东师范大学排名 上海健康医学院排名 华东理工大学排名 上海交通大学排名 同济大学排名 复旦大学排名 上海杉达学院排名 上海戏剧学院排名 上海音乐学院排名