zoukankan      html  css  js  c++  java
  • spring加载多个配置文件

    首先我们都知道要使用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" />
        ……

  • 相关阅读:
    close connection error java.sql.SQLRecoverableException: IO Error: Broken pipe
    Mysql 备份与恢复
    MACBOOK 破解wifi密码
    MAC 安装homebrew
    Linux(CentOS / RHEL 7) 防火墙
    ORA-01031: insufficient privileges
    Oracle登录认证
    ORA-12162: TNS:net service name is incorrectly specified
    lsnrctl: .... cannot restore segment prot after reloc: Permission denied
    CentOS / RHEL 配置yum源
  • 原文地址:https://www.cnblogs.com/gexiaoshan/p/3566238.html
Copyright © 2011-2022 走看看