一、spring中如何使用多个xml配置文件

  1、在web.xml中定义contextConfigLocation参数,Spring会使用这个参数去加载所有逗号分隔的xml文件,如果没有这个参数,spring会默认加载WEB-INF/applicationContext.xml文件(若没有,要新建一个)。

  例如:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:conf/spring/applicationContext_core*.xml,
classpath*:conf/spring/applicationContext_dict*.xml,
classpath*:conf/spring/applicationContext_hibernate.xml,
</param-value>
</context-param>

  contextConfigLocation参数的<param-value>定义了要加载的Spring配置文件。

  PS:classpath指编译后的class路径。包含 WEB-INF/lib下的所有jar包和WEB-INF/classes目录

  原理:利用ServletContextListener实现

  Spring提供ServletContextListener的一个实现类ContextLoaderListener,该类可以作为listener使用,它会在创建时自动查找WEB-INF/下的applicationContext.xml文件。因此,如果只有一个配置文件,并且文件名为applicationContext.xml,则只需要在web.xml文件中增加如下代码即可:

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

  如果有多个配置文件载入,则考虑使用<context-param>元素来确定配置文件的文件名。由于ContextLoaderListener加载时,会查找名为contextConfigLocation的参数(注意:这是底层代码写死的名称),因此,配置<context-param>时参数名字应该是contextConfigLocation。

  带多个配置文件的web.xml文件如下:

<web-app>
  <!--确定多个配置文件-->
  <context-param>
    <!-- 参数名为contextConfigLocation…-->
    <param-name>contextConfigLocation</param-name>
    <!--多个配置文件之间以,隔开-->
    <param-value>/WEB-工NF/daoContext.xml./WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <!-- 采用listener创建ApplicationContext 实例-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

  如果没有contextConfigLocation参数指定配置文件,则Spring自动查找applicationContext.xml配置文件。如果有contextConfigLocation,则利用该参数确定配置文件。改参数的值指定一个字符串,Spring的ContextLoaderListener负责将该字符串分解成多个配置文件,逗号、空格及分号都可以作为字符串的分割符。如果既没有applicationContext.xml文件,也没有使用contextConfigLocation参数确定配置文件,或者contextConfigLocation确定的配置文件不存在,都将导致Spring无法加载配置文件,从而无法正常创建ApplicationContext实例。

二、使用通配符

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>

  比如说用到Hibernate,则把hibernate相关的配置放在applicationContext-hibernate.xml这一个文件,而一些全局相关的信息则放在applicationContext.xml中,其它的配置文件类似,这样就不必用空格或逗号分开多个配置文件了。

【转】web.xml中的contextConfigLocation在spring中的作用的更多相关文章

  1. (转)web.xml中的contextConfigLocation在spring中的作用

    (转)web.xml中的contextConfigLocation在spring中的作用   一.Spring如何使用多个xml配置文件 1.在web.xml中定义contextConfigLocat ...

  2. web.xml中的contextConfigLocation在spring中的作用

    在web.xml中通过contextConfigLocation配置spring, contextConfigLocation参数定义了要装入的 Spring 配置文件.默认会去/WEB-INF/下加 ...

  3. spring web.xml 标签<param-name>contextConfigLocation</param-name>

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

  4. web.xml 详解contextConfigLocation 转

    spring的应用初始化流程一直没有搞明白,刚刚又碰到了相关的问题.决定得好好看看这个流程.我们在开发spring的项目当中基本上都会在web.xml通过: <context-param> ...

  5. spring boot 1.x完整学习指南(含各种常见问题servlet、web.xml、maven打包,spring mvc差别及解决方法)

    spring boot 入门 关于版本的选择,spring boot 2.0开始依赖于 Spring Framework 5.1.0,而spring 5.x和之前的版本差距比较大,而且应该来说还没有广 ...

  6. Spring,SpringMvc配置常见的坑,注解的使用注意事项,applicationContext.xml和spring.mvc.xml配置注意事项,spring中的事务失效,事务不回滚原因

    1.Spring中的applicationContext.xml配置错误导致的异常 异常信息: org.apache.ibatis.binding.BindingException: Invalid ...

  7. web.xml的简单解释以及Hello1中web.xml的简单分析

    一.web.xml的加载过程 ①当我们启动一个WEB项目容器时,容器包括(JBoss,Tomcat等).首先会去读取web.xml配置文件里的配置,当这一步骤没有出错并且完成之后,项目才能正常的被启动 ...

  8. 传值:web.xml传递参数 即在Servlet中获取web.xml里的值

    传值:web.xml传递参数 在web.xml中的Servlet里配置多个init-param <servlet> ... <init-param> <param-nam ...

  9. web.xml之<context-param>与<init-param>的区别与作用【转】

    引用自-->http://www.cnblogs.com/hzj-/articles/1689836.html <context-param>的作用:web.xml的配置中<c ...

随机推荐

  1. Photoshop入门教程图解版

  2. 使用eclipse启动tomcat里的项目时报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    1.这种错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener刚开始看的时候 ...

  3. 【pytorch】pytorch-LSTM

    pytorch-LSTM() torch.nn包下实现了LSTM函数,实现LSTM层.多个LSTMcell组合起来是LSTM. LSTM自动实现了前向传播,不需要自己对序列进行迭代. LSTM的用到的 ...

  4. 微信小程序工具类

    wechat-common-sdk ? 场景:目前工作中的项目需要包含并使用另一个项目. 也许是第三方库,或者你独立开发的,用于多个父项目的库. 现在问题来了:你想要把它们当做两个独立的项目,同时又想 ...

  5. Tomcat启动报错,报找不到gdk_custom.jar

    在 tomcat/conf/context.xml 中新增如下配置 <Context> ... <JarScanner scanManifest="false"/ ...

  6. code runner 使用教程

    https://zhuanlan.zhihu.com/p/54861567 其中解决无法在编辑器中编辑问题(编辑器只读) 只需要把Code-runner: Run In Terminal true(打 ...

  7. magento 2 method config

    1. 模板渲染静态文件: <?php echo $this->getViewFileUrl('requirejs::require.js'); ?> 2.

  8. 「ZJOI Day2」游记

    Day-1 晚上一直在出自己做的模拟赛的T1,真的快要死掉了. 分类讨论几十种情况. 窝还是找了Bluesky大佬一起来验题,她瞬间就A掉了这一道题目...自闭了.. 诶,我还是太弱了. 之前教练组织 ...

  9. git常用命令介绍

    一,仓库 1. git init    把当前目录变成git可管理得仓库,也就是初始化仓库 2. git add 文件名            在仓库中添加新文件 3. git commit -m & ...

  10. css进度条

    1.环形进度条 源码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...