spring源码分析之spring-web web模块分析
0 概述
spring-web的web模塊是更高一層的抽象,它封裝了快速開發(fā)spring-web需要的基礎(chǔ)組件。其結(jié)構(gòu)如下:
1. 初始化Initializer部分
? 1.1 ?Servlet3.0 的ServletContainerInitializer用來支持基于代碼的servlet容器配置,它使用spring的WebApplicationInitializer SPI 來代替(或者混合使用)使用傳統(tǒng)的基于web.xml的方式。
?1.2 ?SpringServletContainerInitializer在兼容servlt 3.0的容器啟動時,觸發(fā)onStartUp方法加載并初始化該類。容器啟動時假定spring web模塊的jar已經(jīng)存放在classpath中。加載時使用ServiceLoader的loader方法來查找spring-web模塊下的META-INF/services/javax.servlet.servletContainerInitializer服務(wù)提供者配置文件。
1.3?HttpRequestHandler接口約等于HttpServlet,所有的方法集中于handleRequest方法。其實現(xiàn)類如下:
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/basicSHOULD 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-cVerbally, 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.5would 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部分的整體結(jié)構(gòu):
是不是有點恍然大悟或者有點小明白了?不錯,accept部分就是負(fù)責(zé)協(xié)商http內(nèi)容的MIME TYPE是否滿足要求的。
3. 數(shù)據(jù)綁定bing部分
? ?在這里,綁定的意思是將不同類型的http請求上的數(shù)據(jù)設(shè)置到特定的對象object上如javabean等,支持multipart的綁定。其結(jié)構(gòu)如下:
綁定支持兩種方式:編程式和注解式。
其中,注解的實現(xiàn)由HandlerMethodInvoker觸發(fā)HandlerMethodResolver來完成。
4.web客戶端client部分
?
?
5. 上下文context部分
包含一系列web應(yīng)用的applicationContext接口和用來啟動根web applicationContext的contextLoaderListener。
各種applicationContext層次結(jié)構(gòu)如下圖:(圖片來源網(wǎng)絡(luò),具體鏈接已經(jīng)找不到了,請原諒)
spring-context相關(guān)內(nèi)容請參照這方面的源碼解析,在這里就不一一贅述了。
6. 過濾器filter
? ? spring也對filter進(jìn)行一定程度的封裝和實現(xiàn),其結(jié)構(gòu)如下:
7. jsf部分
支持將jsf的web層和spring的service集成在一起,并支持jsf的el解析,spring的service層駐留在spring的根webapplicationContext中。
?
8. method部分
? ?提供給spring mvc使用的方法處理類。
9. multipart部分
一個multipart的解決文件上傳的方案框架,MultipartResolver繼承實現(xiàn)了?Apache Commons FileUpload.
?
10. util部分
? ? 提供了公共的工具類。
小結(jié)
? ?spring-web的web模塊是spring框架處理web請求的基礎(chǔ),spring-web的http模塊(http://www.cnblogs.com/davidwang456/p/4421495.html)封裝http協(xié)議中client端/server端的request請求和response響應(yīng)及格式的轉(zhuǎn)換,如json,rss,xml等;spring-web的remoting模塊包括jaxws、caucho、httpinvoker等遠(yuǎn)程調(diào)用;spring-web的web模塊則在上述模塊的基礎(chǔ)上對web應(yīng)用進(jìn)行了進(jìn)一步的封裝,提供了快速開發(fā)web的能力。
注意:上述內(nèi)容為本人在閱讀源碼時的體會和見解,不一定正確,請引用時謹(jǐn)慎,如發(fā)現(xiàn)有錯誤的地方,希望能給我反饋,我會盡快修改。
?
轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/4443942.html
總結(jié)
以上是生活随笔為你收集整理的spring源码分析之spring-web web模块分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jgroup 概述--官方文档
- 下一篇: A beginner’s guide t