首先我们都知道要使用spring,则需要在web.xml中增加如下代码:

web.xml:
1:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>    
 spring是如何加载配置文件肯定也跟 ContextLoaderListener类有关,该类可以作为listener 使用,它会在创建时自动查找WEB-INF/ 下的applicationContext.xrnl 文件。因此,
 如果只有一个配置文件,并且文件名为applicationContext.xml ,则只需在web.xml加上面代码即可。
 如果有多个配置文件需要载入,则考虑使用<context-param>即元素来确定配置文件的文件名。
 由于ContextLoaderListener加载时,会查找名为contextConfigLocation的参数。因此,配置context-param时参数名字应该是
 contextConfigLocation。所以context-param参数的名字是固定的contextConfigLocation.
 如下面的实例:
 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value> /WEB-INF/applicationContext*.xml </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
多个配置文件用","分开,也可以使用通配符"*"加载多个配置文件。如上例!
如果applicationContext.xml文件需要放在src下,在在web.xml中配置如下:
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:applicationContext.xml</param-value>
</context-param>

2:

通过一个父配置文件将所有子配置文件导入
在配置文件中有一个标签import,它能把其它的bean定义配置文件导入到父文件夹中
实例:
web.xml配置:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/config/spring/applicationContext.xml</param-value>
 </context-param>
 <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
在applicationContext.xml配置中:
 <import resource="applicationContext-persistence.xml" />
    ……

spring加载多个配置文件的更多相关文章

  1. 【Java Web开发学习】Spring加载外部properties配置文件

    [Java Web开发学习]Spring加载外部properties配置文件 转载:https://www.cnblogs.com/yangchongxing/p/9136505.html 1.声明属 ...

  2. spring加载多个配置文件如何配置

    为应用指定多个配置文件: 多个配置文件的关系: 并列 包含 并列关系 即有多个配置文件,需要同时加载这多个配置文件: 可以使用可变参数,数组和统配符进行加载: 可变参数 String config1 ...

  3. Spring加载加密的配置文件

    一.继承并实现自己的属性文件配置器类 /** * 带加密的Spring属性配置文件扩展类 * 加密方式:AES * @author simon * */ public class EncryptPro ...

  4. Spring 加载项目外部配置文件

    背景 在项目的部署过程中,一般是打成 war 或者 jar 包,这样一般存在两种问题: 即使是配置文件修改,也还需要整个项目重新打包和部署. 整个项目只有一套环境,不能切换. 针对上面的问题,可以使用 ...

  5. spring加载配置文件

    spring加载配置文件 1.把applicationContext.xml直接放在WEB-INF/classes下,spring会采用默认的加载方式2.采用在web.xml中配置ContextLoa ...

  6. spring加载jar包中多个配置文件(转)

    转自:http://evan0625.iteye.com/blog/1598366 在使用spring加载jar包中的配置文件时,不支持通配符,需要一个一个引入,如下所示: Java代码 <co ...

  7. spring boot 使用不同的profile来加载不同的配置文件

    在开发过程之中,经常需要在开发和测试环境中进行互相切换,当切换的同时需要加载相应的配置文件,因此要经常 性的对配置文件进行相应的修改,长此以往感到十分痛苦.如果能针对开发和测试环境分别建两个不同的配置 ...

  8. Spring加载配置文件的几种方法(org.springframework.beans.factory.BeanDefinitionStoreException)

    一:Spring中的几种容器都支持使用xml装配bean,包括:XmlBeanFactory ,ClassPathXmlApplicationContext ,FileSystemXmlApplica ...

  9. Spring Boot加载application.properties配置文件顺序规则

    SpringApplication会从以下路径加载所有的application.properties文件: 1.file:./config/(当前目录下的config文件夹) 2.file:./(当前 ...

随机推荐

  1. sql where and or优先级 待验证

    where 后面如果有and,or的条件,则or自动会把左右的查询条件分开,即先执行and,再执行or.原因就是:and的执行优先级最高! 关系型运算符优先级高到低为:not and or 问题的解决 ...

  2. 用机器名访问和用Localhost访问时在IE中的区别(备忘)

    meta中未指定文档模式的时候, localhost访问文档模式默认是Edge 机器名访问时文档模式默认是IE7 <head>中添加 <meta http-equiv="X ...

  3. jQuery之Ajax--底层接口

    1. $.ajax()方法:是jQuery最底层的Ajax实现.它的结构为:$.ajax(options).该方法只有一个参数,但在这个对象里面包含了$.ajax()方法所需要的请求设置以及回调函数等 ...

  4. facebook 用curl获取用户资料

    用facebook获取用户信息 $graph_url= "https://graph.facebook.com/me?scope=email&fields=id,name,email ...

  5. BZOJ 3230: 相似子串

    3230: 相似子串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1485  Solved: 361[Submit][Status][Discuss ...

  6. BZOJ2815: [ZJOI2012]灾难

    传送门 学LCA的时候根本没意识到LCA可以有这么多玩法. 这玩意据说是个高级数据结构(支配树)的弱化版,蒟蒻没学过呀.所以出题人提出一个概念叫灾难树. 我理解的灾难树的意思实际上是属于DAG的一个子 ...

  7. Flash调用麦克风

    import flash.events.ActivityEvent;import flash.media.Microphone;var deviceArray:Array = Microphone.n ...

  8. Linux lsof命令 以及 恢复删除的文件

    1.简介 lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以如传 ...

  9. thinkphp 3.2加载类

    基础方式(自动加载) 控制器: public function ff(){ $t = new \Org\Util\Abc(); echo $t->ss(); } ThinkPHP\Library ...

  10. EF 增删改

    一.新增 UserInfo user = new UserInfo() { UserName = "jamsebing", UserPass = " }; db.User ...