zoukankan      html  css  js  c++  java
  • web.xml中如何设置配置文件的加载路径

     web应用程序通过Tomcat等容器启动时,会首先加载web.xml文件,通常我们工程中的各种配置文件,如日志、数据库spring的文件等都在此时被加载,下面是两种常用的配置文件加载路径,即配置文件可以放到 SRC目录下或者可以放到WEB-INF根目录下

    第一种在web.xml中这样配置:
       <context-param>
         <param-name >contextConfigLocation </param-name >
         <param-value >classpath:config/XXXXXXX.xml </param-value >
     </ context-param>
      表示在类路径下有一个名为config的文件夹
    第二种在web.xml中这样配置:
    <context-param>
         <param-name >contextConfigLocation </param-name >
         <param-value >/WEB-INF/config/*-context.xml </param-value >
     </ context-param>
    放在config文件夹下,使用了通配符。两种方式功能一样,使用哪个就看个人喜好了。
     
     
     
    、、、、、、、、、、、、、、、、、、、、

    web.xml 通过contextConfigLocation配置spring 的方式

    SSI框架配置文件路径问题:

    struts2的 1个+N个  路径:src+src(可配置)      名称: struts.xml  + N
    spring 的 1个           路径: src                          名称: applicationContext.xml
    ibatis 的 1个+N个  路径: src+src(可配置)     名称: SqlMapConfig.xml + N

    部署到tomcat后,src目录下的配置文件会和class文件一样,自动copy到应用的 classes目录下

    spring的 配置文件在启动时,加载的是web-info目录下的applicationContext.xml,
    运行时使用的是web-info/classes目录下的applicationContext.xml。

    配置web.xml使这2个路径一致:

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

    多个配置文件的加载

    [html] view plain copy
     
    1. <context-param>  
    2.         <param-name>contextConfigLocation</param-name>  
    3.         <param-value>  
    4.             classpath*:conf/spring/applicationContext_core*.xml,  
    5.             classpath*:conf/spring/applicationContext_dict*.xml,  
    6.             classpath*:conf/spring/applicationContext_hibernate.xml,  
    7.             classpath*:conf/spring/applicationContext_staff*.xml,  
    8.             classpath*:conf/spring/applicationContext_security.xml  
    9.             classpath*:conf/spring/applicationContext_modules*.xml  
    10.             classpath*:conf/spring/applicationContext_cti*.xml  
    11.             classpath*:conf/spring/applicationContext_apm*.xml  
    12.         </param-value>  
    13.     </context-param>  
    contextConfigLocation 参数定义了要装入的 Spring 配置文件。

    首先与Spring相关的配置文件必须要以"applicationContext-"开头,要符合约定优于配置的思想,这样在效率上和出错率上都要好很多。 
    还有最好把所有Spring配置文件都放在一个统一的目录下,如果项目大了还可以在该目录下分模块建目录。这样程序看起来不会很乱。 
    在web.xml中的配置如下: 
    Xml代码 
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:**/applicationContext-*.xml</param-value>  
    </context-param>

    "**/"表示的是任意目录; 
    "**/applicationContext-*.xml"表示任意目录下的以"applicationContext-"开头的XML文件。 
    你自己可以根据需要修改。最好把所有Spring配置文件都放在一个统一的目录下,如:

     <!-- Spring 的配置 -->
     <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:/spring/applicationContext-*.xml</param-value>
     </context-param>

  • 相关阅读:
    【NOIP 模拟赛】钟 模拟+链表
    【NOIP 模拟赛】Evensgn 剪树枝 树形dp
    【NOIP模拟赛】公主的朋友 区间染色问题
    【BZOJ 3669】 [Noi2014]魔法森林 LCT维护动态最小生成树
    【BZOJ3674】可持久化并查集加强版
    【NOIP模拟赛】 permutation 数学(打表)
    【NOIP模拟赛】beautiful 乱搞(平衡树)+ST
    【NOIP模拟赛】与非 乱搞
    【NOIP模拟赛】Evensgn 的债务 乱搞
    [NOIP2009]靶形数独 深搜+枝杈优化
  • 原文地址:https://www.cnblogs.com/ConfidentLiu/p/7141126.html
Copyright © 2011-2022 走看看