<!-- 配置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. Cognos添加关联字段

    (这是另一个表)

  2. (转)SpringMVC学习(十二)——SpringMVC中的拦截器

    http://blog.csdn.net/yerenyuan_pku/article/details/72567761 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter, ...

  3. centos7 取消Ctrl+Alt+Del重启功能

    转载:http://www.cnblogs.com/huangjc/p/4536620.html Linux默认允许任何人按下Ctrl+Alt+Del来重启系统.但是在生产环境中,应该停用按下Ctrl ...

  4. 使用sersync实现实时同步实战

    场景需求: 应用程序会在机器192.168.2.2 /usr/local/news目录中生成一些数据文件,现在需要实时同步到主机192.168.3.3/usr/local/www/cn/news中,同 ...

  5. emmm 深入浅出教你看懂现代金融游戏

    3303只信仰公平[网易陕西省西安网友]1 比特币是骗人的.你们都被“现代帼家纸币”概念茜脑了,而且茜的很彻底,所以你们看不透比特币的骗局.简单来说,现代纸币是“空气纸”,比特币是“空气币(空气数据) ...

  6. Mathematics-基础:1+2+3+……+n

    设Sn=1+2+3+……+n-1+n则有Sn=n+n-1+……+3+2+1两式相加得2Sn=(n+1)+(n+1)+……+(n+1)2Sn=n×(n+1)Sn=n×(n+1)/2

  7. JAVA中等待所有线程都执行结束(转2)

    场景: package com.java4all.mypoint; import java.util.concurrent.CountDownLatch; public class ThreadTes ...

  8. docker 离线安装

    适用于: 1.内网安装docker 2.内网升级docker debian 8 sudo apt-get updatesudo apt-get install -d apt-transport-htt ...

  9. 网易技术分享:Nginx缓存引发的跨域惨案

    推荐:更多技术团队分享文章 关注:MAYOU18技术专栏 1. 前言 贵金属wap版直播间上线后,偶尔有用户反馈,在进入wap直播间的时候,出现空白页面,但是重新刷新又可以正常显示了.我们曾一度认为是 ...

  10. 昨天去面试,这5个Python面试题都被考到了,Python面试题No6

    第1题:字符串的拼接–如何高效的拼接两个字符串? 字符串拼接的几种方法 加号 逗号 直接连接 格式化 join 多行字符串拼接() 加号 print('Python' + 'Plus') 逗号 pri ...