此文来自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. Android开发常用权限设置

    加在AndroidManifest.xml 文件中manifest标签以内,application以外 例如:<!--网络权限 --> <uses-permission androi ...

  2. 【C#复习总结】垃圾回收机制(GC)2

    理解C#垃圾回收机制我们首先说一下CLR(公共语言运行时,Common Language Runtime)它和Java虚拟机一样是一个运行时环境,核心功能包括:内存管理.程序集加载.安全性.异步处理和 ...

  3. Meterpreter常⻅见⽤用法

    0x01 背景 meterpreter作为后渗透模块有多种类型,并且命令由核⼼心命令和扩展库命令组成,极⼤大的丰富了了攻击⽅方式. 需要说明的是meterpreter在漏漏洞洞利利⽤用成功后会发送第二 ...

  4. Paypal2017实习生-软件开发-B卷

    1. [编程|15分] Calculate survival fishes时间限制:1秒空间限制:32768K题目描述Given two zero-indexed arrays A and B con ...

  5. Floyd最短路(带路径输出)

    摘要(以下内容来自百度) Floyd算法又称为插点法,是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法,与Dijkstra算法类似. 该算法名称以创始人之一.1978年图灵奖获得者. ...

  6. 归并排序Python 实现

    一.归并排序   -归并排序(MERGE-SORT)是利用归并的思想实现的排序方法,该算法采用经典的分合策略(将问题分(divide)成一些小的问题然后递归求解,而合的阶段则将分的阶段得到的各答案&q ...

  7. YARN的笔记

  8. VO和DO转换(二) BeanUtils

    VO和DO转换(一) 工具汇总 VO和DO转换(二) BeanUtils VO和DO转换(三) Dozer VO和DO转换(四) MapStruct BeanUtils是Spring提供的,通常项目都 ...

  9. Nginx三部曲(3)SSL

    我们将告诉你 Nginx 的运作模式.蕴含的概念,怎样通过调优 Nginx 来提高应用性能,或是如何设置它的启动和运行. 这个教程有三个部分: 基本概念 —— 这部分需要去了解 Nginx 的一些指令 ...

  10. async并发处理