SpringMVC 中配置文件解读 ApplicationContext vs Web ApplicationContext(ContextLoaderList/DispatcherServlet)

2 篇文章 0 订阅
1 篇文章 0 订阅


1. Tomcat容器

一般Tomcat 服务器下的webapps存放开发web项目
这里写图片描述
从上图可以看出 Tomcat 的容器可分为四个等级,Container、Engine、Host、Context。一般我们所说的Tomcat容器可以简化为图中红虚线框中的内容——即简单看做为一个Servlet容器。对于这个Servlet容器主要由Context来管理。

一个 Context 对应一个 Web 工程(Web工程:就是Tomcat 服务器下的webapps目录下的一个项目),我们可以将这个Context称之为Application Context(一个项目只有一个,负责整个项目上下文环境的管理)。通常一个Servlet class(直接或间接实现了javax.servlet.Servlet接口的类称为Servlet class) 对应一个Wrapper,如果有多个 Servlet 就可以定义多个Wrapper。

启动tomcat时,它会去启动webapps下的web项目,会自动去寻找:项目名\WEB-INF\web.xml 对该文件中配置的参数进行解析,然后进行一系列的初始化动作。创建出该项目对应的Servlet容器,使得该Web项目具备运行环境。

2.SpringMVC—— Application Context vs Web Application Context

在Spring Web应用程序中,有两种类型的context配置文件,每种类型被配置和初始化的方式不同。一个是“Application Context”也被称为 Root Context,另一个是“WebApplicationContext”,也被称为 Child Context

2.1 ApplicationContext

  【1】 ApplicationContext.xml是每个Web应用程序的root context 配置,所以也叫做Root Context
  【2】 Spring加载applicationContext.xml文件并为整个应用程序创建ApplicationContext
  【3】 每个Web应用程序只有一个applicationContext
  【4】 如果您没有使用contextConfigLocation参数在web.xml中显式声明applicationContext配置文件位置,则Spring将搜索WEB-INF文件夹下的applicationContext.xml,并在找不到该文件时抛出FileNotFoundException

ApplicationContext是由web.xml中定义的ContextLoaderListener或ContextLoaderServlet(spring3.0后移除了,改为ContextLoaderListener)初始化的容器,配置如下所示:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>       
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:*-context.xml</param-value>
</context-param>

*-context.xml文件中具体应该配置那些参数呢,后面3.2小节有分析

2.2 WebApplicationContext

  【1】除ApplicationContext之外,在单个Web应用程序中可以有多个WebApplicationContext
  【2】简而言之,在SpringMVC每个DispatcherServlet都与单个WebApplicationContext相关联
  【3】xxx-servlet.xml文件与DispatcherServlet相对应,并且Web应用程序可以配置多个DispatcherServlet来处理请求
  【4】在这种情况下,每个DispatcherServlet都会配置一个单独的xxx-servlet.xml。但是,applicationContext.xml对于所有servlet配置文件都是通用的。
  【5】 Spring将默认从您的webapps 项目下的WEB-INF文件夹加载名为“xxx-servlet.xml”的文件,其中xxx是web.xml中的servlet名称
如果要更改该文件名的名称或更改位置,请将contextConfigLocation作为参数名称添加到initiator-param中。

WebApplicationContext的初始化如下所示:

<servlet>
      <servlet-name>platform-services</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:platform-services-servlet.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
</servlet>

platform-services-servlet.xml文件中具体应该配置那些参数呢,后面3.2小节有简单分析

2.3 ContextLoaderListener

  【1】执行Application Context / Root Context )的实际初始化工作。
  【2】读取contextConfigLocation的context-param 参数并将其值传递给context实例,将其解析为潜在的多个文件路径,这些文件路径可以用任意数量的逗号和空格分隔。例“WEB-INF/applicationContext1.xml,WEB-INF/applicationContext2.xml”
  【3】 ContextLoaderListener是可选的。只需要在这里指出一点:您可以启动Spring应用程序,而无需配置ContextLoaderListener,只需使用DispatcherServlet即可创建基本的最小web.xml

3 Spring web xml配置

3.1 配置主要内容

根据上面介绍,针对SpringMVC web项目web.xml文件中一般会为这个项目配置
  【A】一个Application Context/ Root Context,作用于整个项目。也就是项目启动的一些配置
  【B】一个或多个WebApplicationContext。也就是对用户网页请求进行处理的那部分配置。
  【C】其他 编码/请求过滤等(本文不做分析说明)

3.2要配置什么

  针对【A】,尽然Application Context是针对整个项目,那么就会在里面配置数据源,事务,以及其他整个项目都会用到的一些中间件等
  
  针对【B】,在Spring MVC中,所有传入的请求都会通过一个servlet,这个servlet就是- DispatcherServlet 即前端控制器。 WebApplicationInitializer是Spring MVC提供的一个接口,用来识别代码中的配置,并初始化Servlet容器。前端控制器是Web应用程序开发中的典型设计模式。在这种情况下,单个Servlet接收所有请求并将它们传送给应用程序的所有其他组件。那么DispatcherServlet 中的配置文件主要就是涉及到url映射处理的handler、controllers、view-resolves等的配置。
  
这里写图片描述

4相关文献资料

1. What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?
2.Difference between applicationContext.xml and spring-servlet.xml in Spring Framework
3.Spring ContextLoaderListener And DispatcherServlet Concepts
4.Spring MVC – Application Context vs Web Application Context
5.Servlet 工作原理解析
6.Tomcat 系统架构与设计模式,第 1 部分
7.Role/Purpose of ContextLoaderListener in Spring?
8.Spring ContextLoaderListener And DispatcherServlet Concepts
9.ApplicationContext and ServletContext
10.What is Dispatcher Servlet in Spring?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Casionx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值