SpringMvc的实现原理01

1.从DispatcherServlet开始

public class DispatcherServlet extends FrameworkServlet 

public abstract class FrameworkServlet extends HttpServletBean
 implements ApplicationContextAware

public abstract class HttpServletBean extends HttpServlet
 implements EnvironmentCapable, EnvironmentAware 

Servlet都是从HttpServet的doPost()或者doGet()开始分发;
doService-->doDispatch()

以下是doDispatch方法的源码

(1)用户发送请求 由Dispatcher接受 调用doDispatch方法进行处理
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpServletRequest processedRequest = request;
		HandlerExecutionChain mappedHandler = null;
		boolean multipartRequestParsed = false;

		WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

		try {
			ModelAndView mv = null;
			Exception dispatchException = null;

			try {
				//检查是否是文件上传请求
				processedRequest = checkMultipart(request);
				multipartRequestParsed = (processedRequest != request);

				// Determine handler for the current request.
			
				**//(2)根据当前处理器的映射信息找到处理当前请求的处理器执行链
				//执行链中包含Handler映射信息和拦截器HandlerInteraptor 自定义拦截器需要实现HandlerInteraptor接				    		口或者实现HandlerInterceptorAdapter抽象类 实现 boolean preHandle方法(controller执行前)  void postHandle(渲染也面前)和  void afterCompletion方法(容器关闭前)**
				mappedHandler = getHandler(processedRequest);
				if (mappedHandler == null) {
					noHandlerFound(processedRequest, response);
					return;
				}

				// Determine handler adapter for the current request.
				**//(3) DispatcherServlet接收到HandlerMapping返回的HandlerExcutorChain后,调用HandlerAdapter请求执行具体的Handler(Controller)。**
				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 (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
						return;
					}
				}
				//(4)执行拦截器的preHandle方法
				if (!mappedHandler.applyPreHandle(processedRequest, response)) {
					return;
				}

				// Actually invoke the handler.
				//(5)执行Controller中请求对应的RequestMapping方法  就是执行Controller 返回ModelAndView
				//  1.ModelAttrbuite标注的方法提前运行 返回值存到隐含模型RequestScope中
				//2.确定方法中的请求参数 如果标了@RequestParam注解 就从requestParam中找 找不到就报错
				// 如果没有标注注解 就从隐含模型中拿 如果没有且标了SessionAttrrubite注解就从中拿(标了注解还没有就报错)如果都没有 就利用反射创建一个新的对象
				mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

				if (asyncManager.isConcurrentHandlingStarted()) {
					return;
				}

				applyDefaultViewName(processedRequest, mv);
				//(6)执行拦截器的postHandle
				mappedHandler.applyPostHandle(processedRequest, response, mv);
			}
			catch (Exception ex) {
				dispatchException = ex;
			}
			catch (Throwable err) {
				// As of 4.3, we're processing Errors thrown from handler methods as well,
				// making them available for @ExceptionHandler methods and other scenarios.
				dispatchException = new NestedServletException("Handler dispatch failed", err);
			}
			//(7)渲染页面 
			//1。如果有异常 就是用异常解析器处理异常 返回ModelAndView (自定义异常页面@ControllerAdvice加@ExceptionHandler)
			//2.调用render方法渲染页面 InternalResourceViewResolver根据prefix和suffix得到完整的视图名 渲染页面
			processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
		}
		catch (Exception ex) {
			triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
		}
		catch (Throwable err) {
			triggerAfterCompletion(processedRequest, response, mappedHandler,
					new NestedServletException("Handler processing failed", err));
		}
		finally {
			if (asyncManager.isConcurrentHandlingStarted()) {
				// Instead of postHandle and afterCompletion
				if (mappedHandler != null) {
					mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
				}
			}
			else {
				// Clean up any resources used by a multipart request.
				if (multipartRequestParsed) {
					cleanupMultipart(processedRequest);
				}
			}
		}
	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值