zoukankan      html  css  js  c++  java
  • Spring contextConfigLocation默认加载文件的位置

    在使用Spring框架的时候,如果我们使用的是XML文件配置Bean的方式的话,我们往往会在web.xml里面配置如下内容:

    复制代码
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/spring-bean-config.xml</param-value>
        </context-param>
        
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    复制代码

    这是因为如果我们有配置ContextLoaderListener, Spring会去web.xml中看我们是否有定义contextConfigLocation这个参数,如果有则Spring容器(Bean工厂)会把定义在该xml文件中的bean加载到容器中,那如果没有定义contextConfigLocation参数,Spring会怎么办?web服务器启动的时候会不会报错呢?

    Spring有一个概念就是约定优于配置,也就是说,即使你没有显示定义xml文件的位置,Spring容器会到一个约定的地方去找该文件,如果找不到就要报FileNotFoundException了,我们来看看下面代码段:

    "/WEB-INF/applicationContext.xml" 就是默认的文件地址了,如果你使用了Spring MVC,你肯定要到web.xml里面定义DispatcherServlet

        <servlet>
            <servlet-name>dispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        </servlet>

    这个时候如果你没有显示配置contextConfigLocation参数的话,Spring会到该默认路径下加载配置文件"WEB-INF/dispatcherServlet-servlet.xml", 如果不存在就报异常。

    以上源码存在于XmlWebApplicationContext.

  • 相关阅读:
    BZOJ 2654: tree(二分 最小生成树)
    洛谷P4602 [CTSC2018]混合果汁(主席树)
    SDOI 2018 round2游记
    Codeforces Round #479 (Div. 3) 题解
    软件开发中关于向后兼容的理解
    使用achartengine实现自定义折线图 ----附代码 调试OK
    python每次处理一个字符的三种方法
    子序列的个数 --- 庞果网
    IOS深入学习(4)之Coordinate System
    C# ADO基础 SqlHelper
  • 原文地址:https://www.cnblogs.com/moxiaotao/p/9304910.html
Copyright © 2011-2022 走看看