• Spring:在web.xml正确加载spring配置文件的方式


     

    web.xml加载spring配置文件的方式主要依据该配置文件的名称和存放的位置不同来区别,目前主要有两种方式。

    1. 如果spring配置文件的名称为applicationContext.xml,并且存放在WEB-INF/目录下,那么只需要在web.xml中加入以下代码即可

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

    注意:该监听器会自动扫描WEB-INF/ 下的applicationContext.xrnl 文件,这种方式多数用在只有一个配置文件的情况下。

      还可以使用Spring的ContextLoaderServlet的这个特殊Servlet,实现代码如下

    <servlet>
      <servlet-name>context</servlet-name>
      <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-name>
      <load-on-startup>1(优先级)</load-on-startup>
    </servlet>

    2.如果spring配置文件的名称是自定义的其他名称(我使用的就是自定义名称),比如为applicationContext-test.xml,也还是存放在WEB-INF/目录下,那么还需要配置contextConfigLocation这个参数,该参数是一个字符串,监听器或者Servlet会自定将该字符串按照特定的字符(比如空格、逗号、分号)解析成多个文件。需要添加如下代码:

    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/application-testA.xml,/WEB-INF/application-testB.xml,/WEB-INF/application-testB.xml</param-value>
    </context-param>

    可以使用通配符对上面进行简写如下

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

    注意:如果既没有applicationContext.xml 文件,也没有使用contextConfigLocation参数确定配置文件,或者contextConfigLocation确定的配置文件不存在。都将导致Spring 无法加载配置文件或无法正常创建ApplicationContext 实例.

    加入了Spring监听器,从而才能正确加载Spring配置文件:

    文章转载至:https://www.cnblogs.com/lzxin/p/8992737.html

  • 相关阅读:
    OC 单例实现
    iOS 代码调试
    iOS
    iOS 11 scroll滚动偏移,tableview偏移44,获取view的宽和高
    swift 约束
    OC
    OC 线程操作
    OC 线程操作
    【第35题】2019年OCP认证12C题库062考试最新考试原题-35
    【第34题】2019年OCP认证12C题库062考试最新考试原题-34
  • 原文地址:https://www.cnblogs.com/nhdlb/p/11891178.html
Copyright © 2020-2023  润新知