spring-webmvc

1耍休, 初始化
1.1 web.xml
基于web.xml跷叉,通過ContextLoaderListener在servlet容器啟動(dòng)時(shí)初始化WebApplicationContext顿锰,然后放入ServletContext的WebApplicationContext.class.getName() + ".ROOT"屬性中。

1.2距芬, 基于注解
servlet 3.1 規(guī)范 8.2.4節(jié)
The ServletContainerInitializer class is looked up via the jar services API. For each application, an instance of the ServletContainerInitializer is created by the container at application startup time. The framework providing an implementation of the ServletContainerInitializer MUST bundle in the META-INF/services directory of the jar file a file called javax.servlet.ServletContainerInitializer, as per the jar services API, that points to the implementation class of the ServletContainerInitializer.
In addition to the ServletContainerInitializer we also have an annotation - HandlesTypes. The HandlesTypes annotation on the implementation of the ServletContainerInitializer is used to express interest in classes that may have annotations (type, method or field level annotations) specified in the value of the HandlesTypes or if it extends / implements one those classes anywhere in the class’ super types. The HandlesTypes annotation is applied irrespective of the setting of metadata-complete.

servlet容器在加載web應(yīng)用時(shí)悔常,會(huì)查找META-INF/services文件夾下文件名為javax.servlet.ServletContainerInitializer的文件的內(nèi)容,文件內(nèi)容為ServletContainerInitializer的實(shí)現(xiàn)類想括。HandlesTypes注解指定的類型的所有類會(huì)傳遞給ServletContainerInitializer#onStartup

SpringServletContainerInitializer.png
AbstractContextLoaderInitializer.png

2 陷谱, ContextLoaderListener

initWebApplicationContext.png
configureAndRefreshWebApplicationContext.png

3, DispatcherServlet
3.1 初始化

策略接口.png
initStrategies.png
initHandlerMappings.png
getDefaultStrategies.png
DispatcherServlet.properties

根據(jù)配置瑟蜈,初始化各個(gè)策略接口烟逊。

3.2 doService

doService.png

把FlashMap,WebApplicationContext等放入request

doDispatch.png

doDispatch處理流程
3.2.1 檢查是否文件上傳

checkMultipart.png

3.2.2 根據(jù)handlerMappings和請(qǐng)求獲取HandlerExecutionChain

getHandler.png

從DispatcherServlet.properties可以看出HandlerMapping實(shí)現(xiàn)類為BeanNameUrlHandlerMapping和RequestMappingHandlerMapping

AbstractHandlerMapping

getHandler.png
getHandlerExecutionChain.png

抽象模版設(shè)計(jì)模式:根據(jù)請(qǐng)求獲取handler铺根,加入攔截器鏈宪躯。

BeanNameUrlHandlerMapping的實(shí)現(xiàn)機(jī)制是根據(jù)/開頭的bean別名匹配請(qǐng)求

determineUrlsForHandler.png
detectHandlers.png
registerHandler.png
lookupHandler.png

RequestMappingHandlerMapping的實(shí)現(xiàn)機(jī)制是檢查BeanFactory中所有bean中RequestMapping注解的方法,加入MappingRegistry注冊(cè)表

AbstractHandlerMethodMapping.png
regist.png
getMappingForMethod.png

![createRequestMappingInfo.io/upload_images/11548923-fb373da124807a4f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

查找MappingRegistry注冊(cè)表

lookupHandlerMethod.png

3.2.3 獲取handler之后位迂,查找與之匹配的HandlerAdapter访雪,由HandlerAdapter來處理各種handler,統(tǒng)一返回ModelAndView掂林。

DispatcherServlet.properties中HandlerAdapter的子類為HttpRequestHandlerAdapter臣缀,SimpleControllerHandlerAdapter,RequestMappingHandlerAdapter

HttpRequestHandlerAdapter處理HttpRequestHandler泻帮,不返回ModelAndView精置,自己返回響應(yīng)信息

HttpRequestHandlerAdapter.png

SimpleControllerHandlerAdapter處理實(shí)現(xiàn)Controller接口的handler

SimpleControllerHandlerAdapter.png

RequestMappingHandlerAdapter處理HandlerMethod,根據(jù)注冊(cè)的HandlerMethodReturnValueHandler處理handler返回值锣杂。

AbstractHandlerMethodAdapter.png
supportsInternal.png
invokeHandlerMethod.png
invokeAndHandle.png
handleReturnValue.png
注冊(cè)HandlerMethodReturnValueHandler.png
getDefaultReturnValueHandlers.png

handler返回異步結(jié)果時(shí)才會(huì)異步處理V搿7!

AsyncTaskMethodReturnValueHandler.png

spring-mvc請(qǐng)求處理流程赖阻,很多細(xì)節(jié)還需要完善5骸!火欧!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末棋电,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子苇侵,更是在濱河造成了極大的恐慌离陶,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,681評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件衅檀,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡霎俩,警方通過查閱死者的電腦和手機(jī)哀军,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來打却,“玉大人杉适,你說我怎么就攤上這事×鳎” “怎么了猿推?”我有些...
    開封第一講書人閱讀 169,421評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)捌肴。 經(jīng)常有香客問我蹬叭,道長(zhǎng),這世上最難降的妖魔是什么状知? 我笑而不...
    開封第一講書人閱讀 60,114評(píng)論 1 300
  • 正文 為了忘掉前任秽五,我火速辦了婚禮,結(jié)果婚禮上饥悴,老公的妹妹穿的比我還像新娘坦喘。我一直安慰自己,他們只是感情好西设,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,116評(píng)論 6 398
  • 文/花漫 我一把揭開白布瓣铣。 她就那樣靜靜地躺著,像睡著了一般贷揽。 火紅的嫁衣襯著肌膚如雪棠笑。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,713評(píng)論 1 312
  • 那天擒滑,我揣著相機(jī)與錄音腐晾,去河邊找鬼叉弦。 笑死,一個(gè)胖子當(dāng)著我的面吹牛藻糖,可吹牛的內(nèi)容都是我干的淹冰。 我是一名探鬼主播,決...
    沈念sama閱讀 41,170評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼巨柒,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼樱拴!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起洋满,我...
    開封第一講書人閱讀 40,116評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤晶乔,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后牺勾,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體正罢,經(jīng)...
    沈念sama閱讀 46,651評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,714評(píng)論 3 342
  • 正文 我和宋清朗相戀三年驻民,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了翻具。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,865評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡回还,死狀恐怖裆泳,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情柠硕,我是刑警寧澤工禾,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站蝗柔,受9級(jí)特大地震影響闻葵,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜癣丧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,211評(píng)論 3 336
  • 文/蒙蒙 一笙隙、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧坎缭,春花似錦竟痰、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至憎夷,卻和暖如春莽鸿,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評(píng)論 1 274
  • 我被黑心中介騙來泰國打工祥得, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留兔沃,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,299評(píng)論 3 379
  • 正文 我出身青樓级及,卻偏偏與公主長(zhǎng)得像乒疏,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子饮焦,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,870評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容