<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<!-- DispatcherServlet类的全限定类名 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- DispatcherServlet类的初始化参数 -->
<param-name>contextConfigLocation</param-name>
<!-- 初始化参数的值,即springmvc配置文件的路径 -->
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<!-- 表示web应用启动即加载该servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- servlet-mapping,用于拦截所有请求。即所有请求都拦截到该sevlet -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

上面是在web.xml配置文件中加载springmvc配置文件生成webapplicationcontext容器的经典配置。

我们可以看到,在web应用一启动,该DispatcherServlet就被加载了,加载的时候提供了contextConfigLoacation的初始值,然后通过类的全限定类名使用反射加载该类。

/**
* Set the context config location explicitly, instead of relying on the default
* location built from the namespace. This location string can consist of
* multiple locations separated by any number of commas and spaces.
*/
public void setContextConfigLocation(String contextConfigLocation) {
this.contextConfigLocation = contextConfigLocation;
}

以上代码是在DispatcherSevlet父类FrameworkServlet中,可以看到提供contextConfigloaction后该类就有了srpingmvc配置文件路径。

protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
Class<?> contextClass = getContextClass();
if (this.logger.isDebugEnabled()) {
this.logger.debug("Servlet with name '" + getServletName() +
"' will try to create custom WebApplicationContext context of class '" +
contextClass.getName() + "'" + ", using parent context [" + parent + "]");
}
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException(
"Fatal initialization error in servlet with name '" + getServletName() +
"': custom WebApplicationContext class [" + contextClass.getName() +
"] is not of type ConfigurableWebApplicationContext");
}
ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); wac.setEnvironment(getEnvironment());
wac.setParent(parent);
wac.setConfigLocation(getContextConfigLocation()); configureAndRefreshWebApplicationContext(wac); return wac;
}

以上代码是DispatcherSevlet父类FrameworkServlet中的创造webapplicationcontext的方法,在方法中可以看到获得刚刚设置的springmvc配置文件的路径,并且set到ConfigurableWebApplicationContext用于创建容器。

大体流程就是这样,现在做下小总结:

当web应用启动的时候配置在web.xml中的DisPartcherServlet就被加载,怎么被加载的呢?

通过提供该类的全限定类名和初始化参数使用反射加载。加载后生成了webApplicationContext容器,

通过该容器就可以使用ioc、aop等spring功能了。需要说明的是该容器的生成需要sevlectContext,

因此必须得再拥有web容器的前提下才能加载webApplicationContext容器。因此,即使它继承自

applicationContext,因此它的初始化方式也与applicationContext和BeanFactory不同

web应用启动的时候SpringMVC容器加载过程的更多相关文章

  1. Spring源码:Spring IoC容器加载过程(1)

    Spring源码版本:4.3.23.RELEASE 一.加载过程概览 Spring容器加载过程可以在org.springframework.context.support.AbstractApplic ...

  2. springMVC容器加载源码分析

    springmvc是一个基于servlet容器的轻量灵活的mvc框架,在它整个请求过程中,为了能够灵活定制各种需求,所以提供了一系列的组件完成整个请求的映射,响应等等处理.这里我们来分析下spring ...

  3. springmvc源码分析——入门看springmvc的加载过程

    本文将分析springmvc是如何在容器启动的时候将各个模块加载完成容器的创建的. 我知道在web.xml文件中我们是这样配置springmvc的: 可以看到,springmvc的核心控制器就是Dis ...

  4. springmvc源码分析----入门看springmvc的加载过程

    接上一篇我们写的入门---http://www.cnblogs.com/duanxiaojun/p/6591448.html 今天从这个门里进去我们看springmvc是如何在容器启动的时候将各个模块 ...

  5. Spring源码:Spring IoC容器加载过程(2)

    Spring源码版本:4.3.23.RELEASE 一.加载XML配置 通过XML配置创建Spring,创建入口是使用org.springframework.context.support.Class ...

  6. Java web 加载过程

    1.Web容器初始化过程 2.SpringMVC中web.xml配置 3.认识ServletContextListener 4.认识ContextLoaderListener 5.Dispatcher ...

  7. web.xml 的加载过程

    初始化过程: 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>. 接着容器会创建一个Serv ...

  8. spring 容器加载完成后执行某个方法

    理论 刚好再开发过程中遇到了要在项目启动后自动开启某个服务,由于使用了spring,我在使用了spring的listener,它有onApplicationEvent()方法,在Spring容器将所有 ...

  9. Spring之IOC容器加载初始化的方式

    引言 我们知道IOC容器时Spring的核心,可是如果我们要依赖IOC容器对我们的Bean进行管理,那么我们就需要告诉IOC容易他需要管理哪些Bean而且这些Bean有什么要求,这些工作就是通过通过配 ...

随机推荐

  1. 骑芯供应链(W 笔试)

    单选题 1.调用本身构造方法 答案:this(X) 2.若y=3,a=2,b=4,运行y+=a++/--b,求y的值 答案: 3.不能控制servlet生命周期的方法是? 选项:init().serv ...

  2. MySQL存储过程实现分页及变量的定义

    delimiter是MySQL中的命令,这个命令与存储过程没什么关系. 其实就是告诉mysql解释器,该段命令是否已经结束了,mysql是否可以执行了. 即改变输入结束符. 默认情况下,delimit ...

  3. 【vc】高精度时间函数的使用

    方法一: 函数定义如下: int UsSleep(int us);//返回实际的微秒延时时间 代码实现如下: //参数一表示 需要等待的时间 微秒为单位 int UsSleep(int us) { / ...

  4. Echarts 异步数据加载遇到的问题

    看了Echarts官网异步加载数据的Demo var myChart = echarts.init(document.getElementById('main')); // 显示标题,图例和空的坐标轴 ...

  5. Shell替换数组元素之间的间隔符号

    Shell中的数组是这样表示的: arr=(1,2,3,4,5) 它们数组元素的间隔符号为逗号,如果我相把逗号替换为加号: echo ${arr//,/+} 输出: 1+2+3+4+5 这个表达式是我 ...

  6. poj3710 Christmas Game

    题目描述 题解: 树上删边. 对于奇数长度的环,可以看做一条边. 对于偶数长度的环,可以看做什么都没有. 没有特别好的解释…… 代码: #include<cstdio> #include& ...

  7. Mysql中max函数取得的值不是最大

    ①问题:遇到一个很有意思的问题,这里记录一下, 就是在使用max函数的时候发现取得的最大值其实不是最大值. 比如: 某一列中有10000000,和9999999, 其最大值应该是10000000但是查 ...

  8. nginx+tomcat实现简单的负载均衡

    host1:10.0.0.10(部署nginx和tomcat) host2:10.0.0.11(部署tomcat) 平台环境(2主机一样) [root@smp ~]# uname -r3.10.0-8 ...

  9. RAID磁盘阵列及CentOS7启动流程

    1. 磁盘阵列 1.1 RAID,磁盘阵列磁盘通过硬件和软件的形式组合成一个容量巨大的磁盘组,提升整个磁盘的系统效能:RAID常见类型: RAID类型 最低磁盘个数 空间利用率 各自的优缺点 级别 说 ...

  10. 我的Python分析成长之路1

    Python是什么?                                                                                           ...