SpringMVC執行流程

SpringMVC執行流程圖

在前端控制器中最最重要的方法是 doDispatch,在這個方法中 ,起到委派模式中委派者的角色,負責把 任務分發給各個角色作處理前端

分發的主要任務:java

  1. 獲取處理器映射器ios

  2. 根據處理器映射器獲取處理器適配器瀏覽器

  3. 根據處理器適配器獲取視圖ModelAndViewsession

  4. 使用視圖解析解解析視圖併發

  5. 渲染視圖app

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//把request對象賦值給 processedRequest
		HttpServletRequest processedRequest = request;
		//定義 HandlerExecutionChain 若是是訪問controller方法的話,封裝方法對象(方法對象中封裝了controller對象), HandlerExecutionChain還將封裝全部的攔截器
		HandlerExecutionChain mappedHandler = null;
		boolean multipartRequestParsed = false;

		WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

		try {
			//將會經過 ha.handle(processedRequest, response, mappedHandler.getHandler()); 獲取 ModelAndView
			ModelAndView mv = null;
			Exception dispatchException = null;

			try {
				// 1.嘗試將當前請求轉換爲MultipartHttpServletRequest
				processedRequest = checkMultipart(request);
				multipartRequestParsed = (processedRequest != request);

				
		// 2.查找當前請求對應的handler,包括Handler(控制器也就是controller自己)自己和Handler攔截器

		//當遍歷到requestMappingHandlerMapping時 在requestMappingHandlerMapping中其實存儲了 全部攔截器的對象
				/** * 這個方法一路千辛萬苦,一路封裝,滿載而歸 * 1.首先是調用AbstractHandlerMapping的getHandler方法,而後調用 AbstractHandlerMethodMapping的getHandlerInternal方法 * 而後從新建立建立對象 new HandlerMethod(this, handler) 把controller對象(從工廠中獲取) 賦值 給方法對象HandlerMethod * 因此第一步就是: 把controller對象賦值給方法對象 * * 2.而後調用AbstractHandlerMapping.getHandlerExecutionChain 轉換爲 HandlerExecutionChain 對象 * 遍歷攔截器集合 把全部的攔截器對象賦值給HandlerExecutionChain對象 * 因此第二步就是: 把方法對象轉換爲HandlerExecutionChain對象並把全部的攔截器賦值到其中 * */
				//根據請求request對象,調用處理器映射器尋找處理器,其實就是 HandlerExecutionChain 對象
				mappedHandler = getHandler(processedRequest);
				/** * 此時的 mappedHandler 即爲 HandlerExecutionChain 對象 * HandlerExecutionChain 對象中 封裝了瀏覽器訪問的方法對應的方法對象,方法對象中封裝了對象的controller對象,HandlerExecutionChain封裝了全部的攔截器 */


				// 未能找到對應的handler,拋出NoHandlerFoundException異常並返回404
				if (mappedHandler == null) {
					noHandlerFound(processedRequest, response);
					return;
				}

				// Determine handler adapter for the current request.
				// 3.查找當前請求對應的HandlerAdapter
				//把方法對象傳進去,獲取到一個適配器
				HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

				// Process last-modified header, if supported by the handler.
				// 4.處理last-modified請求頭,若是當前請求支持的話
				//獲取方法的請求方法
				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;
					}
				}

				// 5.應用前置攔截器
				// 若是有攔截器返回false,則代表該攔截器已經處理了返回結果,直接返回; 
                //注意: 此時的 processedRequest 其實就是request 對象
				//就是在判判定義全部的攔截器 的前置方法,返回的究竟是true,仍是false
				//若是有一個前置返回的是false,那麼中止執行下面的代碼, 只有全部的攔截器的前置方法返回的true才能夠
				if (!mappedHandler.applyPreHandle(processedRequest, response)) {
					return;
				}


				// Actually invoke the handler.
				// 6.調用HandlerAdapter的handler方法,真正開始處理Controller

				//在以上的全部步驟中, ModelAndView都尚未返回
				//這個方法嘗試獲取 ModelAndView 對象 把request對象 ,response對象 和方法對象傳進去

				//進入到RequestMappingHandlerAdaper適配器的handleInternal

				//準備獲取ModelAndView對象 同時在方法裏面執行了controller方法的內容
				mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
				//此時ModelAndView對象的view值爲跳轉的路徑

				// 7.若是當前請求是併發處理,直接返回
				if (asyncManager.isConcurrentHandlingStarted()) {
					return;
				}

				// 8. 若是當前返回值中不包含視圖名的話,爲返回值設定默認視圖名,
				//意思若是你沒有設置跳轉路徑的話,這個方法默認給你加跳轉路徑
				applyDefaultViewName(processedRequest, mv);

				// 9.應用已註冊攔截器的後置方法。
				//倒着遍歷全部的攔截器 先註冊的後執行
			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);
			}
			// 10.處理分發調用結果,如視圖模型解析、返回等工做 若是以上有任何錯誤,把錯誤信息封裝賦值給dispatchException 錯誤對象
			//若是dispatchException不爲空的話,打印錯誤信息,若是ModelAndView返回的是一個頁面的話,會從新發起請求
			//若是沒有ModelAndView爲空了 ,或者說controller返回不是一個頁面了, 執行攔截器的後置方法,也是倒着遍歷
			//在這裏面還幹了一個一件事 ,那就是獲取到了 view視圖對象
			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);
				}
			}
		}
	}
複製代碼

聊一下比較重要的幾個方法cors

1.獲取處理器映射器

mappedHandler = getHandler(processedRequest);async

在doDispatch方法中找這個方法,按住Ctrl鍵點擊進入ide

此一路千辛萬苦,一路封裝,滿載而歸

@Nullable
	protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
		//handlerMappings中有5個對象
		//1. favconHandlerMapping 2.requestMappingHandlerMapping 3.beanNameHandlerMapping 4.resourceHandlerMapping 5.welcomePageHandlerMapping
		//遍歷這個五個對象
		if (this.handlerMappings != null) {
			for (HandlerMapping mapping : this.handlerMappings) {
				//當遍歷到requestMappingHandlerMapping時 在requestMappingHandlerMapping中其實存儲了 全部攔截器的對象

				/** * 這個方法一路千辛萬苦,一路封裝,滿載而歸 * 1.首先是調用AbstractHandlerMapping的getHandler方法,而後調用 AbstractHandlerMethodMapping的getHandlerInternal方法 * 而後從新建立建立對象 new HandlerMethod(this, handler) 把controller對象(從工廠中獲取) 賦值 給方法對象HandlerMethod * 因此第一步就是: 把controller對象賦值給方法對象 * * 2.而後調用AbstractHandlerMapping.getHandlerExecutionChain 轉換爲 HandlerExecutionChain 對象 * 遍歷攔截器集合 把攔截器全部的對象賦值給HandlerExecutionChain對象的集合 * 因此第二步就是: 把方法對象轉換爲HandlerExecutionChain對象並把全部的攔截器賦值到其中 * */
				HandlerExecutionChain handler = mapping.getHandler(request);
				if (handler != null) {
					return handler;
				}
			}
		}
		return null;
	}
複製代碼

咱們再看一下

mapping.getHandler(request);方法

按住Ctrl鍵點擊進入,發現是HandlerMapping接口 , 按住快捷鍵 Ctrl + Alt + B 選擇AbstractHandlerMapping 這個實現類

@Override
	@Nullable
	public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
		//獲取Controller對象,可是獲取到時對應方法的對象,方法對象中封裝有controller對象
		//一路獲取controller對象,把controller對象封裝進方法對象中,(前提是訪問的是controller中的方法)
		Object handler = getHandlerInternal(request);
		if (handler == null) {
			handler = getDefaultHandler();
		}
		if (handler == null) {
			return null;
		}
		// Bean name or resolved handler?
		if (handler instanceof String) {
			String handlerName = (String) handler;
			handler = obtainApplicationContext().getBean(handlerName);
		}

         //把方法對象和request傳進去 ,準備把全部的攔截器封裝進 HandlerExecutionChain對象中
		HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);

      // 如今 HandlerExecutionChain 對象中有 方法對象 而方法對象中存儲了controller對象, HandlerExecutionChain中有全部的攔截器對象
		if (logger.isTraceEnabled()) {
			logger.trace("Mapped to " + handler);
		}
		else if (logger.isDebugEnabled() && !request.getDispatcherType().equals(DispatcherType.ASYNC)) {
			logger.debug("Mapped to " + executionChain.getHandler());
		}

		if (CorsUtils.isCorsRequest(request)) {
			CorsConfiguration globalConfig = this.corsConfigurationSource.getCorsConfiguration(request);
			CorsConfiguration handlerConfig = getCorsConfiguration(handler, request);
			CorsConfiguration config = (globalConfig != null ? globalConfig.combine(handlerConfig) : handlerConfig);
			executionChain = getCorsHandlerExecutionChain(request, executionChain, config);
		}

		return executionChain;
	}
複製代碼

2.獲取處理器適配器

HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

咱們再回到 doDispatch方法中,找到該方法, 按住Ctrl鍵點擊進入

//參數爲對應方法對象,或者是頁面資源對象
    protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
		//handlerAdapters 有三個適配器對象
		//1.RquestMappingHandlerAdapter 2.HttpRequestHandlerAdapter 3.SimpleControllerHandlerAdapter
		if (this.handlerAdapters != null) {
			for (HandlerAdapter adapter : this.handlerAdapters) {
			   //查看哪一個處理器符合請求須要的,返回符合條件的處理器
				if (adapter.supports(handler)) {
					return adapter;
				}
			}
		}
		throw new ServletException("No adapter for handler [" + handler +
				"]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");
	}
複製代碼

3.獲取視圖ModelAndView

mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

咱們再回到 doDispatch方法中,找到該方法, 點擊進入

發現是一個HandlerAdapter接口 ,按住快捷鍵 Ctrl + Alt + B ,選擇 AbstractHandlerMethodAdaper

按住Ctrl鍵點擊 handleInternal 方法,發現是一個抽象的方法,再次 按住快捷鍵 Ctrl + Alt + B 進入到RequestMappingHandlerAdapter

@Override
	protected ModelAndView handleInternal(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {

		ModelAndView mav;
		checkRequest(request);

		//若是須要,在同步塊中執行InvokehandlerMethod。
		// Execute invokeHandlerMethod in synchronized block if required.
		if (this.synchronizeOnSession) {
			HttpSession session = request.getSession(false);
			if (session != null) {
				Object mutex = WebUtils.getSessionMutex(session);
				synchronized (mutex) {
					mav = invokeHandlerMethod(request, response, handlerMethod);
				}
			}
			else {
				// No HttpSession available -> no mutex necessary
				mav = invokeHandlerMethod(request, response, handlerMethod);
			}
		}
		else {
			// No synchronization on session demanded at all...
			//嘗試獲取ModelAndView對象,若是沒有HTML,則獲取不到視圖
			//這個方法裏面執行了controller的內容
			mav = invokeHandlerMethod(request, response, handlerMethod);
		}

		if (!response.containsHeader(HEADER_CACHE_CONTROL)) {
			if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
				applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
			}
			else {
				prepareResponse(response);
			}
		}
          //獲取到ModelAndView對象
		return mav;
	}
複製代碼

4.解析視圖

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

咱們再回到 doDispatch方法中,找到該方法, 按住Ctrl鍵點擊進入

private void processDispatchResult(HttpServletRequest request, HttpServletResponse response, @Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv, @Nullable Exception exception) throws Exception {

		boolean errorView = false;

		if (exception != null) {
			if (exception instanceof ModelAndViewDefiningException) {
				logger.debug("ModelAndViewDefiningException encountered", exception);
				mv = ((ModelAndViewDefiningException) exception).getModelAndView();
			}
			else {
				Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
				mv = processHandlerException(request, response, handler, exception);
				errorView = (mv != null);
			}
		}

		//處理程序是否返回要呈現的視圖?
		// Did the handler return a view to render?
		if (mv != null && !mv.wasCleared()) {
			//這個方法當中會獲取到視圖view
			render(mv, request, response);
			if (errorView) {
				WebUtils.clearErrorRequestAttributes(request);
			}
		}
		else {
			if (logger.isTraceEnabled()) {
				logger.trace("No view rendering, null ModelAndView returned.");
			}
		}

		if (WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
			// Concurrent handling started during a forward
			return;
		}

		if (mappedHandler != null) {
			//若是沒有ModelAndView爲空了 ,或者說controller返回不是一個頁面了,執行攔截器的後置方法,也是倒着遍歷
			mappedHandler.triggerAfterCompletion(request, response, null);
		}
	}
複製代碼

找到 render(mv, request, response); 方法, 按住Ctrl鍵點擊進入

protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// Determine locale for request and apply it to the response.
		Locale locale =
				(this.localeResolver != null ? this.localeResolver.resolveLocale(request) : request.getLocale());
		response.setLocale(locale);

		View view;
		String viewName = mv.getViewName();
		if (viewName != null) {
			//咱們須要解析視圖名稱
			// We need to resolve the view name.
			//獲取到了視圖
			view = resolveViewName(viewName, mv.getModelInternal(), locale, request);
			if (view == null) {
				throw new ServletException("Could not resolve view with name '" + mv.getViewName() +
						"' in servlet with name '" + getServletName() + "'");
			}
		}
		else {
			// No need to lookup: the ModelAndView object contains the actual View object.
			view = mv.getView();
			if (view == null) {
				throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " +
						"View object in servlet with name '" + getServletName() + "'");
			}
		}

		// Delegate to the View object for rendering.
		if (logger.isTraceEnabled()) {
			logger.trace("Rendering view [" + view + "] ");
		}
		try {
			if (mv.getStatus() != null) {
				response.setStatus(mv.getStatus().value());
			}
			//獲取到視圖以後
			view.render(mv.getModelInternal(), request, response);
		}
		catch (Exception ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("Error rendering view [" + view + "]", ex);
			}
			throw ex;
		}
	}
複製代碼

這個方法中,解析視圖ModelAndView ,獲取View對象

相關文章
相關標籤/搜索