亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringMVC執(zhí)行過(guò)程詳細(xì)講解

 更新時(shí)間:2022年08月30日 14:41:44   作者:一個(gè)風(fēng)輕云淡  
MVC是一種軟件設(shè)計(jì)典范,用一種業(yè)務(wù)邏輯、數(shù)據(jù)、界面顯示分離的方法組織代碼,將業(yè)務(wù)邏輯聚集到一個(gè)組件里面,在改進(jìn)和個(gè)性化定制界面及用戶交互的同時(shí),不需要重新編寫(xiě)業(yè)務(wù)邏輯,MVC分層有助于管理和架構(gòu)復(fù)雜的應(yīng)用程序

SpringMVC常用組件

DispatcherServlet:前端控制器,不需要工程師開(kāi)發(fā),由框架提供

作用:統(tǒng)一處理請(qǐng)求和響應(yīng),整個(gè)流程控制的中心,由它調(diào)用其它組件處理用戶的請(qǐng)求

HandlerMapping:處理器映射器,不需要工程師開(kāi)發(fā),由框架提供

作用:根據(jù)請(qǐng)求的url、method等信息查找Handler,即控制器方法

Handler:處理器,需要工程師開(kāi)發(fā)

作用:在DispatcherServlet的控制下Handler對(duì)具體的用戶請(qǐng)求進(jìn)行處理

HandlerAdapter:處理器適配器,不需要工程師開(kāi)發(fā),由框架提供

作用:通過(guò)HandlerAdapter對(duì)處理器(控制器方法)進(jìn)行執(zhí)行

ViewResolver:視圖解析器,不需要工程師開(kāi)發(fā),由框架提供

作用:進(jìn)行視圖解析,得到相應(yīng)的視圖,例如:ThymeleafView、InternalResourceView、

RedirectView

View:視圖

作用:將模型數(shù)據(jù)通過(guò)頁(yè)面展示給用戶

DispatcherServlet初始化過(guò)程

DispatcherServlet 本質(zhì)上是一個(gè) Servlet,所以天然的遵循 Servlet 的生命周期。所以宏觀上是 Servlet生命周期來(lái)進(jìn)行調(diào)度。

初始化WebApplicationContext

所在類:org.springframework.web.servlet.FrameworkServle

protected WebApplicationContext initWebApplicationContext() {
WebApplicationContext rootContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null;
if (this.webApplicationContext != null) {
// A context instance was injected at construction time -> use it
wac = this.webApplicationContext;
if (wac instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac =
(ConfigurableWebApplicationContext) wac;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services
such as
// setting the parent context, setting the application context
id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit
parent -> set
// the root application context (if any; may be null) as the
parent
cwac.setParent(rootContext);
}
configureAndRefreshWebApplicationContext(cwac);
}
}
}
if (wac == null) {
// No context instance was injected at construction time -> see if one
// has been registered in the servlet context. If one exists, it is
assumed
// that the parent context (if any) has already been set and that the
// user has performed any initialization such as setting the context id
wac = findWebApplicationContext();
}
if (wac == null) {
// No context instance is defined for this servlet -> create a local one
// 創(chuàng)建WebApplicationContext
wac = createWebApplicationContext(rootContext);
}
if (!this.refreshEventReceived) {
// Either the context is not a ConfigurableApplicationContext with
refresh
// support or the context injected at construction time had already been
// refreshed -> trigger initial onRefresh manually here.
synchronized (this.onRefreshMonitor) {
// 刷新WebApplicationContext
onRefresh(wac);
}
}
if (this.publishContext) {
// Publish the context as a servlet context attribute.
// 將IOC容器在應(yīng)用域共享
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
}
return wac;
}

②創(chuàng)建WebApplicationContext

所在類:org.springframework.web.servlet.FrameworkServlet

protected WebApplicationContext createWebApplicationContext(@Nullable
ApplicationContext parent) {
Class<?> contextClass = getContextClass();
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass))
{
throw new ApplicationContextException(
"Fatal initialization error in servlet with name '" +
getServletName() +
"': custom WebApplicationContext class [" + contextClass.getName() +
"] is not of type ConfigurableWebApplicationContext");
}
// 通過(guò)反射創(chuàng)建 IOC 容器對(duì)象
ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext)
BeanUtils.instantiateClass(contextClass);
wac.setEnvironment(getEnvironment());
// 設(shè)置父容器
wac.setParent(parent);
String configLocation = getContextConfigLocation();
if (configLocation != null) {
wac.setConfigLocation(configLocation);
}
configureAndRefreshWebApplicationContext(wac);
return wac;
}

③DispatcherServlet初始化策略

FrameworkServlet創(chuàng)建WebApplicationContext后,刷新容器,調(diào)用onRefresh(wac),此方法在DispatcherServlet中進(jìn)行了重寫(xiě),調(diào)用了initStrategies(context)方法,初始化策略,即初始化

DispatcherServlet的各個(gè)組件

所在類:org.springframework.web.servlet.DispatcherServlet

protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}

SpringMVC的執(zhí)行流程

用戶向服務(wù)器發(fā)送請(qǐng)求,請(qǐng)求被SpringMVC 前端控制器 DispatcherServlet捕獲。

2) DispatcherServlet對(duì)請(qǐng)求URL進(jìn)行解析,得到請(qǐng)求資源標(biāo)識(shí)符(URI),判斷請(qǐng)求URI對(duì)應(yīng)的映射:

a) 不存在

i. 再判斷是否配置了mvc:default-servlet-handler

ii. 如果沒(méi)配置,則控制臺(tái)報(bào)映射查找不到,客戶端展示404錯(cuò)誤

iii. 如果有配置,則訪問(wèn)目標(biāo)資源(一般為靜態(tài)資源,如:JS,CSS,HTML),找不到客戶端也會(huì)展示404錯(cuò)誤

b) 存在則執(zhí)行下面的流程

3) 根據(jù)該URI,調(diào)用HandlerMapping獲得該Handler配置的所有相關(guān)的對(duì)象(包括Handler對(duì)象以及Handler對(duì)象對(duì)應(yīng)的攔截器),最后以HandlerExecutionChain執(zhí)行鏈對(duì)象的形式返回。

4) DispatcherServlet 根據(jù)獲得的Handler,選擇一個(gè)合適的HandlerAdapter。

5) 如果成功獲得HandlerAdapter,此時(shí)將開(kāi)始執(zhí)行攔截器的preHandler(...)方法【正向】

6) 提取Request中的模型數(shù)據(jù),填充Handler入?yún)?,開(kāi)始執(zhí)行Handler(Controller)方法,處理請(qǐng)求。在填充Handler的入?yún)⑦^(guò)程中,根據(jù)你的配置,Spring將幫你做一些額外的工作:

a) HttpMessageConveter: 將請(qǐng)求消息(如Json、xml等數(shù)據(jù))轉(zhuǎn)換成一個(gè)對(duì)象,將對(duì)象轉(zhuǎn)換為指定的響應(yīng)信息

b) 數(shù)據(jù)轉(zhuǎn)換:對(duì)請(qǐng)求消息進(jìn)行數(shù)據(jù)轉(zhuǎn)換。如String轉(zhuǎn)換成Integer、Double等

c) 數(shù)據(jù)格式化:對(duì)請(qǐng)求消息進(jìn)行數(shù)據(jù)格式化。 如將字符串轉(zhuǎn)換成格式化數(shù)字或格式化日期等

d) 數(shù)據(jù)驗(yàn)證: 驗(yàn)證數(shù)據(jù)的有效性(長(zhǎng)度、格式等),驗(yàn)證結(jié)果存儲(chǔ)到BindingResult或Error中

7) Handler執(zhí)行完成后,向DispatcherServlet 返回一個(gè)ModelAndView對(duì)象。

8) 此時(shí)將開(kāi)始執(zhí)行攔截器的postHandle(...)方法【逆向】。

9) 根據(jù)返回的ModelAndView(此時(shí)會(huì)判斷是否存在異常:如果存在異常,則執(zhí)行

HandlerExceptionResolver進(jìn)行異常處理)選擇一個(gè)適合的ViewResolver進(jìn)行視圖解析,根據(jù)Model

和View,來(lái)渲染視圖。

10) 渲染視圖完畢執(zhí)行攔截器的afterCompletion(...)方法【逆向】。

11) 將渲染結(jié)果返回給客戶端。

到此這篇關(guān)于SpringMVC執(zhí)行過(guò)程詳細(xì)講解的文章就介紹到這了,更多相關(guān)SpringMVC執(zhí)行過(guò)程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論