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 type
parameters from Accept extension parameters is due to historical
practice. Although this prevents any media type parameter named
"q" from being used with a media range, such an event is believed
to be unlikely given the lack of any "q" parameters in the IANA
media type registry and the rare usage of any media type
parameters in Accept. Future media types are discouraged from
registering 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=1
2) text/html
3) 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         = 1
text/html = 0.7
text/plain = 0.3
       image/jpeg                = 0.5
text/html;level=2 = 0.4
text/html;level=3 = 0.7
      Note: A user agent might be provided with a default set of quality
values for certain media ranges. However, unless the user agent is
a 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的能力。

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

spring源码分析之spring-web web模块分析的更多相关文章

  1. Spring源码-IOC部分-Spring是如何解决Bean循环依赖的【6】

    实验环境:spring-framework-5.0.2.jdk8.gradle4.3.1 Spring源码-IOC部分-容器简介[1] Spring源码-IOC部分-容器初始化过程[2] Spring ...

  2. Spring源码-AOP部分-Spring是如何对bean实现AOP代理的

    实验环境:spring-framework-5.0.2.jdk8.gradle4.3.1 历史文章 Spring源码-IOC部分-容器简介[1] Spring源码-IOC部分-容器初始化过程[2] S ...

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

    我们先来看类图吧: 除了BeanFactory这一支的接口,AbstractBeanFactory主要实现了AliasRegistry和SingletonBeanRegistry接口. 这边主要提供了 ...

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

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

  5. 框架源码系列六:Spring源码学习之Spring IOC源码学习

    Spring 源码学习过程: 一.搞明白IOC能做什么,是怎么做的  1. 搞明白IOC能做什么? IOC是用为用户创建.管理实例对象的.用户需要实例对象时只需要向IOC容器获取就行了,不用自己去创建 ...

  6. spring源码学习(三)--spring循环引用源码学习

    在spring中,是支持单实例bean的循环引用(循环依赖)的,循环依赖,简单而言,就是A类中注入了B类,B类中注入了A类,首先贴出我的代码示例 @Component public class Add ...

  7. 小白都能看懂的 Spring 源码揭秘之Spring MVC

    目录 前言 Spring MVC 请求流程 Spring MVC 两大阶段 初始化 HttpServletBean#init() FrameworkServlet#initServletBean Fr ...

  8. (转) Spring源码阅读 之 Spring整体架构

    标签(空格分隔): Spring 声明:本文系转载,原地地址:spring framework 4 源码阅读 Spring骨架 Spring的骨架,也是Spring的核心包.主要包含三个内容 cont ...

  9. 【spring源码学习】spring的远程调用实现源码分析

    [一]spring的远程调用提供的基础类 (1)org.springframework.remoting.support.RemotingSupport ===>spring提供实现的远程调用客 ...

  10. 【spring源码学习】spring集成orm数据框架

    [一]简易的数据源配置 (1)配置文件 <!--springJdbcTemplemate数据操作配置信息 --> <bean id="driver" class= ...

随机推荐

  1. Install_pygments

    安装Pygments语法高亮 On OS X Leopard, Snow Leopard 1 $ sudo easy_install Pygments Alternatively on OS X wi ...

  2. Windows server 2008 上部署 MVC (NopCommerce 3.4)网站

    自己用开源框架做了个商城,该框架是基于mvc4的,本地编译通过,运行一切正常,关于发布遇到了好几个问题. 本地: IIS7.5. VS2013 总结后发现只需要设置两个问题,就不会有那些古怪的问题:什 ...

  3. UVa12657 - Boxes in a Line(数组模拟链表)

    题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...

  4. 第二百四十三天 how can I 坚持

    制定的计划完成不了了,好多问题啊.又想当然了,晚上加了会班. 今天雾霾好严重,一出地铁大裤衩怎么没了.雾霾爆表啊. 还好现在刮大风了. 准备看<芈mi月传>了. 睡觉.

  5. 关闭SELinux的两种方法

    1 永久方法 – 需要重启服务器 修改/etc/selinux/config文件中设置SELINUX=disabled ,然后重启服务器. 2 临时方法 – 设置系统参数 使用命令setenforce ...

  6. 转载github上最全的资源教程--前端涉及的所有知识体系

    以下地址为园子里一个哥们总结的前端所涉及的所有知识体系 http://www.cnblogs.com/bymax/p/5878113.html 很值得学习参考

  7. js隐藏

    function openLoadingIcon(){ $("#dataLoading").css("display","block"); ...

  8. 数据库连接字符串ConnectionString 中的关键字值释义

    ConnectionString 类似于 OLE DB 连接字符串,但并不相同.与 OLE DB 或 ADO不同,如果“Persist Security Info ”值设置为false(默认值),则返 ...

  9. Rstudio编辑界面美化设置

    美化Rstudio的编辑界面有利于我们输入代码,合适的调整更是减少错误. 可以根据自己的喜好和习惯选择.

  10. Cocos2d-x——Cocos2d-x 屏幕适配新解 – 兼容与扩展【转载】

    Cocos2d-x 屏幕适配新解 – 兼容与扩展 本文出自[无间落叶](转载请保留出处):http://blog.leafsoar.com/archives/2013/05-13-08.html 在读 ...