zoukankan      html  css  js  c++  java
  • web.xml 通过contextConfigLocation配置spring 的方式

    部署到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>

    在web.xml中通过contextConfigLocation配置spring,contextConfigLocation 参数定义了要装入的 Spring 配置文件。

    如果想装入多个配置文件,可以在 <param-value> 标记中用逗号作分隔符。
    <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,
        classpath*:conf/spring/applicationContext_staff*.xml,
        classpath*:conf/spring/applicationContext_security.xml
        classpath*:conf/spring/applicationContext_modules*.xml
        classpath*:conf/spring/applicationContext_cti*.xml
        classpath*:conf/spring/applicationContext_apm*.xml
      </param-value>
    </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>

    web.xml中classpath:和classpath*:, 有什么区别?

    classpath:只会到你的class路径中查找找文件;
    classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找. 

    在web.xml里配置Listener

    xml 代码如下: 
       <listener>   
            <listener-class>org.springframework.web.context.ContextLoaderListener<listener-class>   
       </listener>    

    如果在web.xml里给该Listener指定要加载的xml,如:

    xml代码如下:

    <!-- spring config -->

    <context-param>

      <param-name>contextConfigLocation</param-name>  

      <param-value>classpath:applicationContext.xml</param-value>

    </context-param>

    则会去加载相应的xml,而不会去加载/WEB-INF/下的applicationContext.xml。

    但是,如果没有指定的话,默认会去/WEB-INF/下加载applicationContext.xml。

  • 相关阅读:
    C#通过“委托和事件”的方式实现进程监控并与“普通方式”对比
    SharePoint自动化系列——通过PowerShell创建SharePoint Web
    SharePoint自动化系列——通过PowerShell创建SharePoint Site Collection
    IE11不支持Selenium 2.0的解决方法
    C#中WebBrowser控件的使用
    SharePoint自动化系列——Solution auto-redeploy using Selenium(C#)
    多进程监控自动关机工具升级远程关闭多台server——C# works with PowerShell
    通过Windows PowerShell远程管理计算机(精简版)
    .NET项目web自动化测试实战——Selenium 2.0
    HP LoadRunner 12.02 Tutorial T7177-88037教程独家中文版
  • 原文地址:https://www.cnblogs.com/Neil223/p/3369379.html
Copyright © 2011-2022 走看看