此文来自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. Java发送Email邮件及SpringBoot集成

    一:普通方式发送 1.导包 <!--Java MAil 发送邮件API--> <dependency> <groupId>javax.mail</groupI ...

  2. HashMap是如何工作的

    目录 1 HashMap在JAVA中的怎么工作的? 2 什么是哈希? 3 HashMap 中的 Node 类 4 键值对在 HashMap 中是如何存储的 5 哈希碰撞及其处理 6 HashMap 的 ...

  3. C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 增强服务安全、阻止非授权的用户非法调用

    多一道防线,多一些安全保障,当程序发布到互联网上,再有成千上万的用户在用,总会有各种牛人出现,万一遇到破坏分子,那会有灾难性的打击. 只要跟利益有关系的,跟资金有关系,跟财务有关系,有竞争对手,软件系 ...

  4. 第十二届湖南省赛 A - 2016 ( 数学,同余转换)

    给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量:       1. 1≤a≤n,1≤b≤m;   2. a×b 是 2016 的倍数.   Input   输入包含不超过 30 ...

  5. Shell脚本2

      5 Shell传递参数 我们可以在执行 Shell 脚本时,向脚本传递参数, 脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… ...

  6. shell脚本--初识CGI

    CGI按照百度百科的定义,如下: CGI 是Web 服务器运行时外部程序的规范,按CGI 编写的程序可以扩展服务器功能.CGI 应用程序能与浏览器进行交互,还可通过数据库API 与数据库服务器等外部数 ...

  7. Mysql连接数、线程数、数据包

    https://blog.csdn.net/qq_26545305/article/details/79675507

  8. Dockers 学习

    Docker镜像操作,有五个常用的命令: a.拉取镜像,后跟镜像仓库名称,如果要指定某个版本,可以带上tag. > docker pull <repo>[:tag] b.列出所有镜像 ...

  9. js 深度复制deepClone

    function isObject(obj) { return typeof obj === 'object' && obj != null; } const deepClone =( ...

  10. vue bug & data type bug

    vue bug & data type bug [Vue warn]: Invalid prop: type check failed for prop "value". ...