此文来自https://my.oschina.net/pkpk1234/blog/61971

(写的特别好)故引来借鉴

Spring MVC启动过程

以Tomcat为例,想在Web容器中使用Spirng MVC,必须进行四项的配置:
修改web.xml,添加servlet定义、编写servletname-servlet.xml(
servletname是在web.xm中配置DispactherServlet时使servlet-name的值)
、配置contextConfigLocation初始化参数、配置ContextLoaderListerner。

<!-- servlet定义 -->

<servlet>

<servlet-name>court</servlet-name>

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

<load-on-startup>1</load-on-startup>

</servlet>


<servlet-mapping>

<servlet-name>court</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<!-- 配置contextConfigLocation初始化参数 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/court-service.xml</param-value>

</context-param>

<!-- 配置ContextLoaderListerner -->

<listener>

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

</listener>

DispatcherServlet:前端处理器,接受的HTTP请求和转发请求的类。

court-servlet.xml:定义WebAppliactionContext上下文中的bean。

contextConfigLocation:指定Spring IoC容器需要读取的定义了非web层的Bean(DAO/Service)的XML文件路径。

ContextLoaderListener:Spring MVC在Web容器中的启动类,负责Spring IoC容器在Web上下文中的初始化。

Spring MVC启动过程大致分为两个过程:1、ContextLoaderListener初始化,实例化IoC容器,并将此容器实例注册到ServletContext中。2、DispatcherServlet初始化。

ContextLoaderListener初始化


Web容器调用contextInitialized方法初始化ContextLoaderListener,在此方法中,ContextLoaderListener通过调用继承自ContextLoader的initWebApplicationContext方法实例化Spring
Ioc容器。

initWebApplicationContext方法进行的操作如图1:


以上在实例化Spring IoC容器的过程中,最主要的两个方法是createWebApplicationContext和configureAndRefreshWebApplicationContext方法。

createWebApplicationContext方法用于返回XmlWebApplicationContext实例,即Web环境下的Spring IoC容器。

configureAndRefreshWebApplicationContext用于配置XmlWebApplicationContext,读取web.xml中通过contextConfigLocation标签指定的XML文件,实例化XML文件中配置的bean,并在上一步中实例化的容器中进行注册。


完成以上两步的操作后,Spring
MVC会将XmlWebApplicationContext实例以属性的方式注册到ServletContext中,属性的名称由WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE指定,默认值为:WebApplicationContext.class.getName()
+ ".ROOT"。此Spring 容器是ROOT上下文,供所有的Spring MVC Servlcet使用。

Spring MVC启动过程(1):ContextLoaderListener初始化的更多相关文章

  1. spring mvc 启动过程及源码分析

    由于公司开源框架选用的spring+spring mvc + mybatis.使用这些框架,网上都有现成的案例:需要那些配置文件.每种类型的配置文件的节点该如何书写等等.如果只是需要项目能够跑起来,只 ...

  2. Spring MVC启动过程

    org.springframework.web.context.ContextLoaderListener ContextLoaderListener的作用就是启动Web容器时,自动装配Applica ...

  3. spring MVC 运行过程

    以Tomcat为例,想在Web容器中使用Spirng MVC,必须进行四项的配置: 1.修改web.xml, 2.添加servlet定义.编写servletname-servlet.xml( serv ...

  4. Spring Boot启动过程(七):Connector初始化

    Connector实例的创建已经在Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动中提到了: Connector是LifecycleMBeanBase的子类,先是设置L ...

  5. 转:spring的启动过程-spring和springMVC父子容器的原理

    要想很好理解这三个上下文的关系,需要先熟悉spring是怎样在web容器中启动起来的.spring的启动过程其实就是其IoC容器的启动过程,对于web程序,IoC容器启动过程即是建立上下文的过程. s ...

  6. Spring MVC启动流程分析

    本文是Spring MVC系列博客的第一篇,后续会汇总成贴子. Spring MVC是Spring系列框架中使用频率最高的部分.不管是Spring Boot还是传统的Spring项目,只要是Web项目 ...

  7. Spring MVC 上下文(ApplicationContext)初始化入口

    Spring 常用上下文容器有哪些 ApplicationContext ClassPathXmlApplicationContext ApplicationContext context = new ...

  8. Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动

    之前在Spring Boot启动过程(二)提到过createEmbeddedServletContainer创建了内嵌的Servlet容器,我用的是默认的Tomcat. private void cr ...

  9. Spring Boot启动过程及回调接口汇总

    Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...

随机推荐

  1. 深入浅出Tomcat/3 - Tomcat生命周期

    在上面的部分,其实我们已经接触到Tomcat的生命周期了,接下来我们将仔细讨论和学习Tomcat的生命周期的具体实现. LifeCycle接口 这个LifeCycle接口上面在讲解Server和Ser ...

  2. C# Type.GetType 返回NULL 问题解决记录

    Type.GetType("OP.Client.Html.Resources.KenFengFormMethod"); 从Dll里面获取KenFengFormMethod这个会返回 ...

  3. nodejs简单模仿web.net web api

    最近用了asp.net web api + EF开发一个项目,但是移植到linux时遇到问题(mono只支持EF6.0,但是mysql驱动不支持EF6.0).所以决定换个思路,用nodejs实现res ...

  4. Can’t connect to local MySQL server through socket 原因解析

    在连接mysql的时,经常会出现以下错误提示: ERROR (HY000): Can't connect to local MySQL server through socket '/var/lib/ ...

  5. redis的应用场景 为什么用redis

    一.不是万能的菲关系系数据库redis 在面试的时候,常被问比较下Redis与Memcache的优缺点,个人觉得这二者并不适合一起比较,redis:是非关系型数据库不仅可以做缓存还能干其它事情,Mem ...

  6. rest-framework解析器,url控制,分页,响应器,渲染器,版本控制

    解析器 1.json解析器 发一个json格式的post请求.后台打印: request_data---> {'title': '北京折叠'} request.POST---> <Q ...

  7. 硬盘扩容9999T

    win+r运行创建命令:subst H: d:\123说明:H指的是想要创建的盘符,d:\123是文件路径 删除命令subst H: d/说明 :H指的是已创建的盘符,/d指的是删除的意思 注意新盘符 ...

  8. 解读event.returnValue和return false

    前言 首先我们要清楚returnValue是IE的一个属性,如果设置了该属性,它的值比事件句柄的返回值优先级要高,把它的值设置为false,可以取消发生事件源元素的默认动作:return false就 ...

  9. MyEclipse10 复制之前的项目部署到tomcat时项目名称对不上,还是复制前的项目名称,哪里修改设置

    工程 -- 右键属性 -- Myeclispse -- web修改一下发布名字就可以了.

  10. Azure系列2.1.6 —— BlobProperties

    (小弟自学Azure,文中有不正确之处,请路过各位大神指正.) 网上azure的资料较少,尤其是API,全是英文的,中文资料更是少之又少.这次由于公司项目需要使用Azure,所以对Azure的一些学习 ...