0 概述

spring-web的web模块是更高一层的抽象,它封装了快速开发spring-web需要的基础组件。其结构如下:

1. 初始化Initializer部分

1.1  Servlet3.0 的ServletContainerInitializer用来支持基于代码的servlet容器配置,它使用spring的WebApplicationInitializer SPI 来代替(或者混合使用)使用传统的基于web.xml的方式。

1.2  SpringServletContainerInitializer在兼容servlt 3.0的容器启动时,触发onStartUp方法加载并初始化该类。容器启动时假定spring web模块的jar已经存放在classpath中。加载时使用ServiceLoader的loader方法来查找spring-web模块下的META-INF/services/javax.servlet.servletContainerInitializer服务提供者配置文件。

1.3 HttpRequestHandler接口约等于HttpServlet,所有的方法集中于handleRequest方法。其实现类如下:

2. 接受请求的accept部分

   还记得我们的http请求报文吗?

GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1 (Request line)
Host: net.tutsplus.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120
Pragma: no-cache
Cache-Control: no-cache

先了解一下rfc-2616是如何定义的?

The Accept request-header field can be used to specify certain media types which are acceptable for the response. Accept headers can be used to indicate that the request is specifically limited to a small set of desired types, as in the case of a request for an in-line image.

       Accept         = "Accept" ":"#( media-range [ accept-params ] )
       media-range    = ( "*/*"| ( type "/" "*" )| ( type "/" subtype )) *( ";" parameter )accept-params  = ";" "q" "=" qvalue *( accept-extension )accept-extension = ";" token [ "=" ( token | quoted-string ) ]

The asterisk "*" character is used to group media types into ranges, with "*/*" indicating all media types and "type/*" indicating all subtypes of that type. The media-range MAY include media type parameters that are applicable to that range.

Each media-range MAY be followed by one or more accept-params, beginning with the "q" parameter for indicating a relative quality factor. The first "q" parameter (if any) separates the media-range parameter(s) from the accept-params. Quality factors allow the user or user agent to indicate the relative degree of preference for that media-range, using the qvalue scale from 0 to 1 (section 3.9). The default value is q=1.

      Note: Use of the "q" parameter name to separate media typeparameters from Accept extension parameters is due to historicalpractice. Although this prevents any media type parameter named"q" from being used with a media range, such an event is believedto be unlikely given the lack of any "q" parameters in the IANAmedia type registry and the rare usage of any media typeparameters in Accept. Future media types are discouraged fromregistering any parameter named "q".

The example

       Accept: audio/*; q=0.2, audio/basic

SHOULD be interpreted as "I prefer audio/basic, but send me any audio type if it is the best available after an 80% mark-down in quality."

If no Accept header field is present, then it is assumed that the client accepts all media types. If an Accept header field is present, and if the server cannot send a response which is acceptable according to the combined Accept field value, then the server SHOULD send a 406 (not acceptable) response.

A more elaborate example is

       Accept: text/plain; q=0.5, text/html,text/x-dvi; q=0.8, text/x-c

Verbally, this would be interpreted as "text/html and text/x-c are the preferred media types, but if they do not exist, then send the text/x-dvi entity, and if that does not exist, send the text/plain entity."

Media ranges can be overridden by more specific media ranges or specific media types. If more than one media range applies to a given type, the most specific reference has precedence. For example,

       Accept: text/*, text/html, text/html;level=1, */*

have the following precedence:

       1) text/html;level=12) text/html3) text/*4) */*

The media type quality factor associated with a given type is determined by finding the media range with the highest precedence which matches that type. For example,

       Accept: text/*;q=0.3, text/html;q=0.7, text/html;level=1,text/html;level=2;q=0.4, */*;q=0.5

would cause the following values to be associated:

       text/html;level=1         = 1text/html                 = 0.7text/plain                = 0.3
       image/jpeg                = 0.5text/html;level=2         = 0.4text/html;level=3         = 0.7
      Note: A user agent might be provided with a default set of qualityvalues for certain media ranges. However, unless the user agent isa closed system which cannot interact with other rendering agents,this default set ought to be configurable by the user.

我们再看accept部分的整体结构:

是不是有点恍然大悟或者有点小明白了?不错,accept部分就是负责协商http内容的MIME TYPE是否满足要求的。

3. 数据绑定bing部分

在这里,绑定的意思是将不同类型的http请求上的数据设置到特定的对象object上如javabean等,支持multipart的绑定。其结构如下:

绑定支持两种方式:编程式和注解式。

其中,注解的实现由HandlerMethodInvoker触发HandlerMethodResolver来完成。

4.web客户端client部分

5. 上下文context部分

包含一系列web应用的applicationContext接口和用来启动根web applicationContext的contextLoaderListener。

各种applicationContext层次结构如下图:(图片来源网络,具体链接已经找不到了,请原谅)

spring-context相关内容请参照这方面的源码解析,在这里就不一一赘述了。

6. 过滤器filter

spring也对filter进行一定程度的封装和实现,其结构如下:

7. jsf部分

支持将jsf的web层和spring的service集成在一起,并支持jsf的el解析,spring的service层驻留在spring的根webapplicationContext中。

8. method部分

提供给spring mvc使用的方法处理类。

9. multipart部分

一个multipart的解决文件上传的方案框架,MultipartResolver继承实现了 Apache Commons FileUpload.

10. util部分

提供了公共的工具类。

小结

spring-web的web模块是spring框架处理web请求的基础,spring-web的http模块(http://www.cnblogs.com/davidwang456/p/4421495.html)封装http协议中client端/server端的request请求和response响应及格式的转换,如json,rss,xml等;spring-web的remoting模块包括jaxws、caucho、httpinvoker等远程调用;spring-web的web模块则在上述模块的基础上对web应用进行了进一步的封装,提供了快速开发web的能力。

注意:上述内容为本人在阅读源码时的体会和见解,不一定正确,请引用时谨慎,如发现有错误的地方,希望能给我反馈,我会尽快修改。

转载于:https://www.cnblogs.com/davidwang456/p/4443942.html

spring源码分析之spring-web web模块分析相关推荐

  1. 人人都能看懂的Spring源码解析,Spring如何解决循环依赖

    人人都能看懂的Spring源码解析,Spring如何解决循环依赖 原理解析 什么是循环依赖 循环依赖会有什么问题? 如何解决循环依赖 问题的根本原因 如何解决 为什么需要三级缓存? Spring的三级 ...

  2. 【Spring 源码阅读】Spring IoC、AOP 原理小总结

    Spring IoC.AOP 原理小总结 前言 版本约定 正文 Spring BeanFactory 容器初始化过程 IoC 的过程 bean 完整的创建流程如下 AOP 的过程 Annotation ...

  3. 【Spring源码学习】Spring Bean的销毁

    [Spring源码学习]Spring Bean的销毁 一.注册bean销毁的类 1.registerDisposableBeanIfNecessary() 2.DisposableBeanAdapte ...

  4. Spring源码解析 - AbstractBeanFactory 实现接口与父类分析

    2019独角兽企业重金招聘Python工程师标准>>> 我们先来看类图吧: 除了BeanFactory这一支的接口,AbstractBeanFactory主要实现了AliasRegi ...

  5. spring源码深度解析—Spring的整体架构和环境搭建

    概述 Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.Spring是于2003 年兴起的一个轻量级的Java 开发框 ...

  6. spring源码学习:spring初始化流程

    首先借个图,说明一下spring的bean的整个生命流程. 销毁什么的这个看图就知道怎么回事,使用的话一般都是纯业务,而且我们更关心spring是怎么初始化的,初始化成我们定义的那个样子.我们就是以这 ...

  7. (转)spring源码解析,spring工作原理

    转自:https://www.ibm.com/developerworks/cn/java/j-lo-spring-principle/ Spring 的骨骼架构 Spring 总共有十几个组件,但是 ...

  8. Spring 源码阅读 之 Spring框架加载

    说起第一次阅读Spring Framework源码,大概还是2010年吧,那个时候还不懂技巧和方法,一头扎在代码的汪洋大海里,出不来了.后面几年偶尔断断续续的也看过几次,都是不得要领,最后都是无疾而终 ...

  9. 【Spring源码系列】Spring注解扫描-@ComponentScan底层原理解读

    这里写目录标题 前言 一.Spring扫描-@ComponentScan注解介绍 @ComponentScan作用 @ComponentScan重要参数 二.Spring扫描-源码分析 声明关键点 源 ...

  10. 闷棍暴打面试官 Spring源码系列: (一) Spring 如何解决循环依赖

    前言 初夏时节, 时间: AM 7.30分左右, 空无一人的健身房里,一个硕大的身体在跑步机上扭动着, 不一会头上便挥汗如雨, 他嘴上还不时嘀咕着 "循环依赖,单例模式,Bean的定位加载注 ...

最新文章

  1. C++ Primer 读书笔记 - 第十三章
  2. 【12c新特性】12c中如何自动启动PDB Pluggable Database
  3. Java 9 中的 GC 调优基础
  4. 稳定婚姻问题:Gale–Shapley算法
  5. MySQL报错this is incompatible with sql_mode=only_full_group_by
  6. 笨办法学 Python · 续 练习 6:`find`
  7. 扩展 HashMap
  8. 自动登录Windows系统
  9. 1091.二进制矩阵中的最短路径(力扣leetcode) 博主可答疑该问题
  10. Java 正则表达式的用法和实例
  11. 【LeetCode-面试算法经典-Java实现】【012-Integer to Roman(数字转罗马字符)】
  12. Java实现输出PDF
  13. # Vue 组件开发打包、Vue 项目打包、js库组件库打包使用
  14. java 小技巧_成为JAVA高手的25个小窍门
  15. 二维码——数字保险箱
  16. 动态刷新listview数据
  17. 对二叉树堆排序的升级TOPK问题(跑路人笔记)
  18. oracle查看锁定任务
  19. 喜闻乐见之Activity生命周期
  20. 数据 术语_这5个必须知道的数据科学家进入零售领域的术语

热门文章

  1. win7下python的安装与配置_Win7下Python与Tensorflow-CPU版开发环境的安装与配置过程...
  2. java 用来查找输出的函数_Solr复杂查询一:函数查询
  3. 青岛互联网java开发_为什么说Java是过去未来的互联网编程(上)
  4. mysql charindex_mysql中替代charindex的函数substring_index、find_in_set | 学步园
  5. 任务间资源共享问题示例
  6. nod找不到服务器,node.js – 带有nodejs child_process的ssh,在服务器上找不到命令
  7. 第六代微型计算机是,AMD Carrizo第六代A系列处理器技术解析
  8. CCF 2015年题目题解 - Python
  9. Python 画樱花(动态画+飘落效果+暗色效)
  10. jetson nano 实现车牌识别