SpringMVC的默认配置文件位置

先说结论 :
Root容器的默认配置文件为/WEB-INF/applicationContext.xml.

Mvc容器的默认配置文件为/WEB-INF/${dispatcherServletName}-servlet.xml.

结论可能大家都知道, 但为什么这样呢? “知其然, 并知其所以然”, 程序员的基本素质


1. 分清springMVC的两个容器

首先要分清springMVC由两个WebApplicationContext容器.

第一个是ContextLoaderListener配置的父容器, 下文简称Root容器. (如果web.xml中没有配置则没有该容器)

第二个是由DispatcherServlet完成初始化SpringMVC相关的容器, 下文简称Mvc容器.

且Root容器是Mvc容器的Parent, 啥意思? 就是Root容器的bean能被Mvc容器的bean引用, 反之不行.

在这里插入图片描述


2. 由ContextLoaderListener初始化的Root容器

如果web.xml中没有配置ContextLoaderListener, 则没有该容器.

正常情况下web.xml中会配置contextConfigLocation来指定Root容器的配置文件位置.

其中param-Value可以用*通配符, 如servletContext-*.xml来配置

也可以用classpath和classpath*来配置(区别是classpath只匹配到第一个)

也可以配置多个指定位置, 可以用逗号, 分号, 以及换行符来隔开, 如context-rpc.xml,context-mq.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:service-appcontext.xml</param-value>
</context-param>

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

但是如果不小心, 忘记配置context-param, 则取默认配置/WEB-INF/applicationContext.xml. 依据是什么呢?

直接上代码: XmlWebApplicationContext.java

protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {
        String[] configLocations = this.getConfigLocations();
        if (configLocations != null) {
            String[] arr$ = configLocations;
            int len$ = configLocations.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String configLocation = arr$[i$];
                reader.loadBeanDefinitions(configLocation);
            }
        }
    }

 protected String[] getConfigLocations() {
        return this.configLocations != null ? this.configLocations : this.getDefaultConfigLocations();
    }

    protected String[] getDefaultConfigLocations() {
       //nameSpace未设置为null
        return this.getNamespace() != null ? new String[]{"/WEB-INF/" + this.getNamespace() + ".xml"} : new String[]{"/WEB-INF/applicationContext.xml"};
    }

因为此时Root容器的namespace未设置, j即为null, 所以默认位置为/WEB-INF/applicationContext.xml.


3. 由DispatcherServlet初始化的Mvc容器

Mvc容器的配置文件由web.xml中DispatcherServlet的init-param确定. 如下:

<servlet>
        <servlet-name>webServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:web-appcontext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

如果DispatcherServlet未配置contextConfigLocation, 则默认取/WEB-INF/${dispatcherServletName}-servlet.xml

为什么呢? 因为该容器的类型也是XmlWebApplicationContext, 和Root容器唯一的差别是Mvc容器的namespace不为null, 而是为${dispatcherServletName}-servlet, 源码在哪里呢?

答案在FrameworkServlet.java(DispatcherServlet的父类)里:

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
        wac.setServletContext(this.getServletContext());
        wac.setServletConfig(this.getServletConfig());
        wac.setNamespace(this.getNamespace());//就是这一行
        wac.addApplicationListener(new SourceFilteringListener(wac, new FrameworkServlet.ContextRefreshListener()));
        ConfigurableEnvironment env = wac.getEnvironment();
        if (env instanceof ConfigurableWebEnvironment) {
            ((ConfigurableWebEnvironment)env).initPropertySources(this.getServletContext(), this.getServletConfig());
        }

        this.postProcessWebApplicationContext(wac);
        this.applyInitializers(wac);
        wac.refresh();
}

public String getNamespace() {
        return this.namespace != null ? this.namespace : this.getServletName() + "-servlet";
}


备注: 以上代码SpringMVC的版本为3.2.16.RELEASE

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值