博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMvc流程分析,简单源码分析
阅读量:6671 次
发布时间:2019-06-25

本文共 5720 字,大约阅读时间需要 19 分钟。

SpringMvc的请求入口:web.xml中的DispatcherServlet

springServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:spring-mvc.xml
1
springServlet
*.html

调用DispatcherServlet的doService方法

protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {        if (logger.isDebugEnabled()) {            String resumed = WebAsyncUtils.getAsyncManager(request).hasConcurrentResult() ? " resumed" : "";            logger.debug("DispatcherServlet with name '" + getServletName() + "'" + resumed +                    " processing " + request.getMethod() + " request for [" + getRequestUri(request) + "]");        }        // Keep a snapshot of the request attributes in case of an include,        // to be able to restore the original attributes after the include.        Map
attributesSnapshot = null; if (WebUtils.isIncludeRequest(request)) { attributesSnapshot = new HashMap
(); Enumeration
attrNames = request.getAttributeNames(); while (attrNames.hasMoreElements()) { String attrName = (String) attrNames.nextElement(); if (this.cleanupAfterInclude || attrName.startsWith("org.springframework.web.servlet")) { attributesSnapshot.put(attrName, request.getAttribute(attrName)); } } } // Make framework objects available to handlers and view objects. request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext()); request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver); request.setAttribute(THEME_RESOLVER_ATTRIBUTE, this.themeResolver); request.setAttribute(THEME_SOURCE_ATTRIBUTE, getThemeSource()); FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(request, response); if (inputFlashMap != null) { request.setAttribute(INPUT_FLASH_MAP_ATTRIBUTE, Collections.unmodifiableMap(inputFlashMap)); } request.setAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap()); request.setAttribute(FLASH_MAP_MANAGER_ATTRIBUTE, this.flashMapManager); try { doDispatch(request, response); } finally { if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) { // Restore the original attribute snapshot, in case of an include. if (attributesSnapshot != null) { restoreAttributesAfterInclude(request, attributesSnapshot); } } } }

调用DispatcherServlet的doDispatch方法,

processedRequest = checkMultipart(request);                multipartRequestParsed = (processedRequest != request);                // Determine handler for the current request.                mappedHandler = getHandler(processedRequest);                if (mappedHandler == null || mappedHandler.getHandler() == null) {                    noHandlerFound(processedRequest, response);                    return;                }                // Determine handler adapter for the current request.                HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());                // Process last-modified header, if supported by the handler.                String method = request.getMethod();                boolean isGet = "GET".equals(method);                if (isGet || "HEAD".equals(method)) {                    long lastModified = ha.getLastModified(request, mappedHandler.getHandler());                    if (logger.isDebugEnabled()) {                        logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);                    }                    if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {                        return;                    }                }                if (!mappedHandler.applyPreHandle(processedRequest, response)) {                    return;                }                // Actually invoke the handler.                mv = ha.handle(processedRequest, response, mappedHandler.getHandler());                if (asyncManager.isConcurrentHandlingStarted()) {                    return;                }                applyDefaultViewName(processedRequest, mv);                mappedHandler.applyPostHandle(processedRequest, response, mv);

获取mappedHandler,mappedHandler里面已经有了具体的Controller和方法

// Determine handler for the current request.                mappedHandler = getHandler(processedRequest);                if (mappedHandler == null || mappedHandler.getHandler() == null) {                    noHandlerFound(processedRequest, response);                    return;                }

获取HandlerAdapter

// Determine handler adapter for the current request.                HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

方法执行前的intercepter,比如aop拦截

if (!mappedHandler.applyPreHandle(processedRequest, response)) {                    return;                }

执行方法

// Actually invoke the handler.                mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

方法结束后的intercepter

mappedHandler.applyPostHandle(processedRequest, response, mv);

解析结果,对报错和视图处理

processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);

总结

1、请求进入DispatchServlet

2、通过handlermapping找到对应的handler
3、找到handlerAdapter
4、执行pre Intercepter
5、执行具体的方法
6、执行post Intercepter
7、解析视图返回

转载于:https://www.cnblogs.com/zhangbin1989/p/9306319.html

你可能感兴趣的文章
OA系统软件怎么选型?
查看>>
英伟达发布Tesla P4&P40两款基于Pascal架构的深度学习芯片
查看>>
《Web应用漏洞侦测与防御:揭秘鲜为人知的攻击手段和防御技术》——1.5 Web Worker...
查看>>
《UNIX网络编程 卷1:套接字联网API(第3版)》——8.10 UDP程序例子小结
查看>>
拯救 Firefox !
查看>>
《人工智能:计算Agent基础》——2.2 Agent系统
查看>>
Firefox 53 Beta 引入两个新 “Compact” 主题
查看>>
英特尔增强 Android 安全 提高自家芯片吸引力
查看>>
《腾讯iOS测试实践》一一1.2 工程效率
查看>>
《Photoshop Lightroom4 经典教程》—第1课1.1节了解Lightroom的工作方式
查看>>
《数据科学:R语言实现》——第2章 数据抽取、转换和加载
查看>>
《深入理解Spark:核心思想与源码分析》——3.7节创建和启动DAGScheduler
查看>>
《ANSYS Workbench有限元分析实例详解(静力学)》——2.5 Windows界面相应操作
查看>>
《R与Hadoop大数据分析实战》一1.7 Hadoop的子项目
查看>>
Google Web Designer 开始支持 Linux
查看>>
《电路分析导论(原书第12版)》一第3章 电阻
查看>>
设计师应该学习业务而非编写代码
查看>>
《代码整洁之道:程序员的职业素养》一一1.3 首先,不行损害之事
查看>>
《音乐达人秀:Adobe Audition实战200例》——1.2 从双卡录音机到多轨录音软件
查看>>
《运营力——微信公众号 设计 策划 客服 管理 一册通》导读
查看>>