SpringMVC执行流程

SpringMVC执行流程


一、SpringMVC流程执行图

在这里插入图片描述

二、三大组件

SpringMVC三大组件为:HandlerMapping(处理器映射器)、HandlerAdapter(处理器适配器)、ViewResolver(视图解析器)

HandlerMapping: 根据请求的url查找Handler
HandlerAdapter: 按照特定规则(HandlerAdapter要求的规则)去执行Handler
ViewResolver: 进行视图解析,根据逻辑视图名解析成真正的视图(view)

三、源码分析

3.1 doDispatch源码分析

首先我们看下DispatcherServlet类,该类继承FrameworkServlet ,FrameworkServlet 继承HttpServletBean,HttpServletBean继承HttpServlet类,看到HttpServlet类,我们就知道肯定要重写doPost和doGet方法,我们发现FrameworkServlet 中重写了该方法:

@Override
protected final void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {

	processRequest(request, response);
}

@Override
protected final void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {

	processRequest(request, response);
}

发现不论是doGet还是doPost最终都是调用processRequest() 点进去

protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {

	long startTime = System.currentTimeMillis();
	Throwable failureCause = null;

	LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
	LocaleContext localeContext = buildLocaleContext(request);

	RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
	ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);

	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());

	initContextHolders(request, localeContext, requestAttributes);

	try {
		//doService为抽象方法 会调用子类方法 
		doService(request, response);
	}
	catch (ServletException | IOException ex) {
		failureCause = ex;
		throw ex;
	}
	catch (Throwable ex) {
		failureCause = ex;
		throw new NestedServletException("Request processing failed", ex);
	}

	finally {
		resetContextHolders(request, previousLocaleContext, previousAttributes);
		if (requestAttributes != null) {
			requestAttributes.requestCompleted();
		}
		logResult(request, response, failureCause, asyncManager);
		publishRequestHandledEvent(request, response, startTime, failureCause);
	}
}

protected abstract void doService(HttpServletRequest request, HttpServletResponse response)
			throws Exception;

进入到DispatcherServlet类的doService方法后 前边代码不用看 直接看doDispatch()核心方法

@Override
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
	logRequest(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<String, Object> 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(DEFAULT_STRATEGIES_PREFIX)) {
				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());

	if (this.flashMapManager != null) {
		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);
			}
		}
	}
}

这时候我们看到核心代码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);
			//获取处理器映射器执行链 执行链中封装得有拦截器 处理器(controller)
			//获取所有的HandlerMapping(处理器映射器) 循环遍历 根据请求url以及请求方式找到合适得处理器映射器 将处理器封装到处理器映射器执行链返回
			//注意 HandlerMapping只封装匹配规则 真正执行处理器是HandlerAdapter
			mappedHandler = getHandler(processedRequest);
			if (mappedHandler == null) {
				noHandlerFound(processedRequest, response);
				return;
			}
			//根据执行链中的处理器找到能执行该处理器的处理器适配器 
			//如何找:循环遍历容器中的处理器适配器 看适配器是否支持该处理器
			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;
				}
			}
			//执行链中如果有拦截器 那么会执行拦截器中的PreHandle方法 
			//PreHandle方法为正序执行 如果都执行成功 放行 否则 直接return 
			if (!mappedHandler.applyPreHandle(processedRequest, response)) {
				return;
			}
			//处理器适配器开始调用处理器执行请求
			//也就是执行我们写的controller方法 返回一个ModelAndView对象
			mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
			if (asyncManager.isConcurrentHandlingStarted()) {
				return;
			}
			//如果没返回ModelAndView对象 则设置默认得MV对象
			applyDefaultViewName(processedRequest, mv);
			//执行链中如果有拦截器 那么会执行拦截器中的PostHandle方法 
			//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);
		}
		//根据ModelAndView对象来让视图解析器来解析视图 解析完视图对象进行页面渲染 最终返回给浏览器
		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);
			}
		}
	}
}

3.1 请求映射源码分析

我们要想知道springmvc是如何帮我们匹配到具体的处理器,那么我们先知道这些处理器映射器哪里来的 我们看到DispatcherServlet类中有个初始化方法

protected void initStrategies(ApplicationContext context) {
	initMultipartResolver(context);
	initLocaleResolver(context);
	initThemeResolver(context);
	//初始化处理器映射器 从容器中获取 我们自己也可以定义
	initHandlerMappings(context);
	//初始化处理器适配器 从容器中获取 我们自己也可以定义
	initHandlerAdapters(context);
	initHandlerExceptionResolvers(context);
	initRequestToViewNameTranslator(context);
	initViewResolvers(context);
	initFlashMapManager(context);
}

然后我们再看刚才的源码 其中有一行为:
mappedHandler = getHandler(processedRequest);
然后我们进入该方法

protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
	if (this.handlerMappings != null) {
		//遍历所有映射器
		for (HandlerMapping mapping : this.handlerMappings) {
			HandlerExecutionChain handler = mapping.getHandler(request);
			if (handler != null) {
				return handler;
			}
		}
	}
	return null;
}

//进入getHandler

public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
	//主要步骤1:找到能处理请求的处理器
	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);
	}
	//主要步骤二:将处理器封装到处理器执行链中 
	HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);

	if (logger.isTraceEnabled()) {
		logger.trace("Mapped to " + handler);
	}
	else if (logger.isDebugEnabled() && !request.getDispatcherType().equals(DispatcherType.ASYNC)) {
		logger.debug("Mapped to " + executionChain.getHandler());
	}

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

	return executionChain;
}

//我们先看下步骤一:点进去

protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
	//解析请求路径
	String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
	request.setAttribute(LOOKUP_PATH, lookupPath);
	this.mappingRegistry.acquireReadLock();
	try {
		//实际调用这个方法来查找的
		HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);
		return (handlerMethod != null ? handlerMethod.createWithResolvedBean() : null);
	}
	finally {
		this.mappingRegistry.releaseReadLock();
	}
}
//进入lookupHandlerMethod
protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
       List<Match> matches = new ArrayList<>();
       /**
        * mappingRegistry是一个注册表 进入该方法后是 return this.urlLookup.get(urlPath);
        * urlLookup 的声明为: MultiValueMap<String, T> urlLookup = new LinkedMultiValueMap<>();
        * List<T> directPathMatches 是从注册表中获取到的Mapping
        * 然后根据addMatchingMappings方法找到合适的RequestMappingInfo 构建出新的RequestMappingInfo
        * 然后根据注册表中的mappingLookup 获取到RequestMappingInfo 对应的 HandlerMethod 
        * 将新的RequestMappingInfo和HandlerMethod 封装到match进行最佳匹配
        */
       List<T> directPathMatches = this.mappingRegistry.getMappingsByUrl(lookupPath);
       if (directPathMatches != null) {
           addMatchingMappings(directPathMatches, matches, request);
       }
       if (matches.isEmpty()) {
           // No choice but to go through all mappings...
           addMatchingMappings(this.mappingRegistry.getMappings().keySet(), matches, request);
       }

       if (!matches.isEmpty()) {
           //获取第一个Match
           Match bestMatch = matches.get(0);
           //如果有多个 匹配最佳 如果请求方式也一样 那么抛出异常
           if (matches.size() > 1) {
               Comparator<Match> comparator = new MatchComparator(getMappingComparator(request));
               matches.sort(comparator);
               bestMatch = matches.get(0);
               if (logger.isTraceEnabled()) {
                   logger.trace(matches.size() + " matching mappings: " + matches);
               }
               if (CorsUtils.isPreFlightRequest(request)) {
                   return PREFLIGHT_AMBIGUOUS_MATCH;
               }
               Match secondBestMatch = matches.get(1);
               if (comparator.compare(bestMatch, secondBestMatch) == 0) {
                   Method m1 = bestMatch.handlerMethod.getMethod();
                   Method m2 = secondBestMatch.handlerMethod.getMethod();
                   String uri = request.getRequestURI();
                   throw new IllegalStateException(
                           "Ambiguous handler methods mapped for '" + uri + "': {" + m1 + ", " + m2 + "}");
               }
           }
           request.setAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE, bestMatch.handlerMethod);
           handleMatch(bestMatch.mapping, lookupPath, request);
           return bestMatch.handlerMethod;
       }
       else {
           return handleNoMatch(this.mappingRegistry.getMappings().keySet(), lookupPath, request);
       }
   }

//那么我们自己写的@requestMaping是怎么找到的呢
//进入到addMatchingMappings

private void addMatchingMappings(Collection<T> mappings, List<Match> matches, HttpServletRequest request) {
	for (T mapping : mappings) {
		//再进去
		T match = getMatchingMapping(mapping, request);
		if (match != null) {
			matches.add(new Match(match, this.mappingRegistry.getMappings().get(mapping)));
		}
	}
}
protected RequestMappingInfo getMatchingMapping(RequestMappingInfo info, HttpServletRequest request) {
	//再进去
	return info.getMatchingCondition(request);
}

public RequestMappingInfo getMatchingCondition(HttpServletRequest request) {
	RequestMethodsRequestCondition methods = this.methodsCondition.getMatchingCondition(request);
	if (methods == null) {
		return null;
	}
	ParamsRequestCondition params = this.paramsCondition.getMatchingCondition(request);
	if (params == null) {
		return null;
	}
	HeadersRequestCondition headers = this.headersCondition.getMatchingCondition(request);
	if (headers == null) {
		return null;
	}
	ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(request);
	if (consumes == null) {
		return null;
	}
	ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(request);
	if (produces == null) {
		return null;
	}
	PatternsRequestCondition patterns = this.patternsCondition.getMatchingCondition(request);
	if (patterns == null) {
		return null;
	}
	RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(request);
	if (custom == null) {
		return null;
	}

	return new RequestMappingInfo(this.name, patterns,
			methods, params, headers, consumes, produces, custom.getCondition());
}

//最终看到都被封装成了一个RequestMappingInfo对象 那么我们看下这个类 其实这个类就是封装我们@RequestMapping注解中一些信息的

public final class RequestMappingInfo implements RequestCondition<RequestMappingInfo> {

	@Nullable
	private final String name;
	//封装注解中值
	private final PatternsRequestCondition patternsCondition;
	//封装注解中的method方法
	private final RequestMethodsRequestCondition methodsCondition;
	//封装注解中参数
	private final ParamsRequestCondition paramsCondition;
	//封装注解中请求头
	private final HeadersRequestCondition headersCondition;
	
	private final ConsumesRequestCondition consumesCondition;
	
	private final ProducesRequestCondition producesCondition;
	
	private final RequestConditionHolder customConditionHolder;
	//以下代码省略
}
//看到这些我们明白了 就是将@RequestMapping()注解里面的值封装到RequestMappingInfo里面,
//在容器启动的时候会将这些标注解@RequestMapping的RequestMappingInfo中,再将该对象封装到MappingRegistry注册表中  方便后续匹配使用

步骤二:
protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) {
	//构建执行链 
	HandlerExecutionChain chain = (handler instanceof HandlerExecutionChain ?
			(HandlerExecutionChain) handler : new HandlerExecutionChain(handler));
	//根据url匹配拦截器 封装到执行链中 返回给DispatcherServlet
	String lookupPath = this.urlPathHelper.getLookupPathForRequest(request, LOOKUP_PATH);
	for (HandlerInterceptor interceptor : this.adaptedInterceptors) {
		if (interceptor instanceof MappedInterceptor) {
			MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor;
			if (mappedInterceptor.matches(lookupPath, this.pathMatcher)) {
				chain.addInterceptor(mappedInterceptor.getInterceptor());
			}
		}
		else {
			chain.addInterceptor(interceptor);
		}
	}
	return chain;
}

3.2 处理器执行流程源码分析

在3.1中我们走完了mappedHandler = getHandler(processedRequest);这个流程,接下来就是中央调度器去将执行连交给处理器适配器去执行

HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
//进入该方法
protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
	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");
}

//可以看到此时是遍历所有得处理器适配器 找到能支持处理器得处理器适配器
//找到之后适配器开始执行处理器
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
//进入
public final ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
		throws Exception {

	return handleInternal(request, response, (HandlerMethod) handler);
}
//再进去 直接看重要部分

protected ModelAndView handleInternal(HttpServletRequest request,
		HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
	//处理器执行完返回给调度器得试图对象
	ModelAndView mav;
	checkRequest(request);

	// 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...
		mav = invokeHandlerMethod(request, response, handlerMethod);
	}

	if (!response.containsHeader(HEADER_CACHE_CONTROL)) {
		if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
			applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
		}
		else {
			prepareResponse(response);
		}
	}

	return mav;
}
//来到invokeHandlerMethod方法
protected ModelAndView invokeHandlerMethod(HttpServletRequest request,
		HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {

	ServletWebRequest webRequest = new ServletWebRequest(request, response);
	try {
		WebDataBinderFactory binderFactory = getDataBinderFactory(handlerMethod);
		ModelFactory modelFactory = getModelFactory(handlerMethod, binderFactory);

		ServletInvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(handlerMethod);
		//设置请求参数解析器
		if (this.argumentResolvers != null) {
			invocableMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
		}
		//设置返回值处理器
		if (this.returnValueHandlers != null) {
			invocableMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);
		}
		invocableMethod.setDataBinderFactory(binderFactory);
		invocableMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
		//ModelAndView容器 保存ModelAndView 
		ModelAndViewContainer mavContainer = new ModelAndViewContainer();
		mavContainer.addAllAttributes(RequestContextUtils.getInputFlashMap(request));
		modelFactory.initModel(webRequest, mavContainer, invocableMethod);
		mavContainer.setIgnoreDefaultModelOnRedirect(this.ignoreDefaultModelOnRedirect);

		AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request, response);
		asyncWebRequest.setTimeout(this.asyncRequestTimeout);

		WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
		asyncManager.setTaskExecutor(this.taskExecutor);
		asyncManager.setAsyncWebRequest(asyncWebRequest);
		asyncManager.registerCallableInterceptors(this.callableInterceptors);
		asyncManager.registerDeferredResultInterceptors(this.deferredResultInterceptors);

		if (asyncManager.hasConcurrentResult()) {
			Object result = asyncManager.getConcurrentResult();
			mavContainer = (ModelAndViewContainer) asyncManager.getConcurrentResultContext()[0];
			asyncManager.clearConcurrentResult();
			LogFormatUtils.traceDebug(logger, traceOn -> {
				String formatted = LogFormatUtils.formatValue(result, !traceOn);
				return "Resume with async result [" + formatted + "]";
			});
			invocableMethod = invocableMethod.wrapConcurrentResult(result);
		}
		//进入到该方法
		invocableMethod.invokeAndHandle(webRequest, mavContainer);
		if (asyncManager.isConcurrentHandlingStarted()) {
			return null;
		}

		return getModelAndView(mavContainer, modelFactory, webRequest);
	}
	finally {
		webRequest.requestCompleted();
	}
}

//进入 invocableMethod.invokeAndHandle(webRequest, mavContainer);
public void invokeAndHandle(ServletWebRequest webRequest, ModelAndViewContainer mavContainer,
		Object... providedArgs) throws Exception {
	//方法1:invokeForRequest 将请求得值传入我们写的处理器中 然后执行处理器 获取处理器执行完得结果
	Object returnValue = invokeForRequest(webRequest, mavContainer, providedArgs);
	setResponseStatus(webRequest);

	if (returnValue == null) {
		if (isRequestNotModified(webRequest) || getResponseStatus() != null || mavContainer.isRequestHandled()) {
			disableContentCachingIfNecessary(webRequest);
			mavContainer.setRequestHandled(true);
			return;
		}
	}
	else if (StringUtils.hasText(getResponseStatusReason())) {
		mavContainer.setRequestHandled(true);
		return;
	}

	mavContainer.setRequestHandled(false);
	Assert.state(this.returnValueHandlers != null, "No return value handlers");
	try {
		//方法2:handleReturnValue 将返回得结果封装为我们想要得对象
		this.returnValueHandlers.handleReturnValue(
				returnValue, getReturnValueType(returnValue), mavContainer, webRequest);
	}
	catch (Exception ex) {
		if (logger.isTraceEnabled()) {
			logger.trace(formatErrorForReturnValue(returnValue), ex);
		}
		throw ex;
	}
}

//我们先看下方法1 看mvc是怎么将我们传入得参数转换为对应得类型 然后传入到我们得方法上
public Object invokeForRequest(NativeWebRequest request, @Nullable ModelAndViewContainer mavContainer, Object... providedArgs) throws Exception {
	//args为为请求传入得值 接着进入这个方法
    Object[] args = this.getMethodArgumentValues(request, mavContainer, providedArgs);
    if (this.logger.isTraceEnabled()) {
        this.logger.trace("Arguments: " + Arrays.toString(args));
    }
	//此处就是调用我们写在处理器中方法 将ars传入到我们自己写的方法内
    return this.doInvoke(args);
 }

/**
* 确定传入的参数的值
*/
protected Object[] getMethodArgumentValues(NativeWebRequest request, @Nullable ModelAndViewContainer mavContainer, Object... providedArgs) throws Exception {
   //获取方法参数数组 标注了目标方法的注解 里边有参数类型 参数注解等参数详细信息
   MethodParameter[] parameters = this.getMethodParameters();
   if (ObjectUtils.isEmpty(parameters)) {
       return EMPTY_ARGS;
   } else {
       //创建参数值对象数组
       Object[] args = new Object[parameters.length];

       for(int i = 0; i < parameters.length; ++i) {
           MethodParameter parameter = parameters[i];
           parameter.initParameterNameDiscovery(this.parameterNameDiscoverer);
           args[i] = findProvidedArgument(parameter, providedArgs);
           if (args[i] == null) {
               //判断是否有参数解析器是否能支持解析参数 如何判断:看该参数标注了哪个注解 例如标注了@RequestParam注解  将支持解析的参数解析器存到缓存中 方便下次直接使用
               if (!this.resolvers.supportsParameter(parameter)) {
                   throw new IllegalStateException(formatArgumentError(parameter, "No suitable resolver"));
               }

               try {
                   //解析参数 各个标注注解对应的都有相应的参数解析器 由参数解析器去解析 然后获取请求传入的参数的值 接着往下走
                   args[i] = this.resolvers.resolveArgument(parameter, mavContainer, request, this.dataBinderFactory);
               } catch (Exception var10) {
                   if (this.logger.isDebugEnabled()) {
                       String exMsg = var10.getMessage();
                       if (exMsg != null && !exMsg.contains(parameter.getExecutable().toGenericString())) {
                           this.logger.debug(formatArgumentError(parameter, exMsg));
                       }
                   }

                   throw var10;
               }
           }
       }

       return args;
   }
}

public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
	//获取能解析的参数解析器
    HandlerMethodArgumentResolver resolver = this.getArgumentResolver(parameter);
    if (resolver == null) {
        throw new IllegalArgumentException("Unsupported parameter type [" + parameter.getParameterType().getName() + "]. supportsParameter should be called first.");
    } else {
    	//进入  这个方法由很多实现类 我们看一下我们常用的一个RequestResponseBodyMethodProcessor
        return resolver.resolveArgument(parameter, mavContainer, webRequest, binderFactory);
    }
}

public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {

	parameter = parameter.nestedIfOptional();
	//进入
	Object arg = readWithMessageConverters(webRequest, parameter, parameter.getNestedGenericParameterType());
	String name = Conventions.getVariableNameForParameter(parameter);

	if (binderFactory != null) {
		WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name);
		if (arg != null) {
			validateIfApplicable(binder, parameter);
			if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
				throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
			}
		}
		if (mavContainer != null) {
			mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
		}
	}

	return adaptArgumentIfNecessary(arg, parameter);
}
protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter,
		Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {

	HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
	Assert.state(servletRequest != null, "No HttpServletRequest");
	ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(servletRequest);

	Object arg = readWithMessageConverters(inputMessage, parameter, paramType);
	if (arg == null && checkRequired(parameter)) {
		throw new HttpMessageNotReadableException("Required request body is missing: " +
				parameter.getExecutable().toGenericString(), inputMessage);
	}
	return arg;
}
/**
*这里就是利用消息转换器将我们传入的参数 转换为我们需要的类型以及结果
*/
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter parameter,
		Type targetType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {
	//媒体类型 浏览器请求头中携带 例如我们常用的application/json;
	MediaType contentType;
	boolean noContentType = false;
	try {
		contentType = inputMessage.getHeaders().getContentType();
	}
	catch (InvalidMediaTypeException ex) {
		throw new HttpMediaTypeNotSupportedException(ex.getMessage());
	}
	if (contentType == null) {
		noContentType = true;
		contentType = MediaType.APPLICATION_OCTET_STREAM;
	}

	Class<?> contextClass = parameter.getContainingClass();
	Class<T> targetClass = (targetType instanceof Class ? (Class<T>) targetType : null);
	if (targetClass == null) {
		ResolvableType resolvableType = ResolvableType.forMethodParameter(parameter);
		targetClass = (Class<T>) resolvableType.resolve();
	}

	HttpMethod httpMethod = (inputMessage instanceof HttpRequest ? ((HttpRequest) inputMessage).getMethod() : null);
	Object body = NO_VALUE;

	EmptyBodyCheckingHttpInputMessage message;
	try {
		message = new EmptyBodyCheckingHttpInputMessage(inputMessage);
		//遍历消息转换器 看那种消息转换器可以支持转换我们浏览器发送的请求格式
		//然后用消息转换器将我们的值转换为我们方法上类型以及参数的值
		for (HttpMessageConverter<?> converter : this.messageConverters) {
			Class<HttpMessageConverter<?>> converterType = (Class<HttpMessageConverter<?>>) converter.getClass();
			GenericHttpMessageConverter<?> genericConverter =
					(converter instanceof GenericHttpMessageConverter ? (GenericHttpMessageConverter<?>) converter : null);
			if (genericConverter != null ? genericConverter.canRead(targetType, contextClass, contentType) :
					(targetClass != null && converter.canRead(targetClass, contentType))) {
				if (message.hasBody()) {
					HttpInputMessage msgToUse =
							getAdvice().beforeBodyRead(message, parameter, targetType, converterType);
					body = (genericConverter != null ? genericConverter.read(targetType, contextClass, msgToUse) :
							((HttpMessageConverter<T>) converter).read(targetClass, msgToUse));
					body = getAdvice().afterBodyRead(body, msgToUse, parameter, targetType, converterType);
				}
				else {
					body = getAdvice().handleEmptyBody(null, message, parameter, targetType, converterType);
				}
				break;
			}
		}
	}
	catch (IOException ex) {
		throw new HttpMessageNotReadableException("I/O error while reading input message", ex, inputMessage);
	}

	if (body == NO_VALUE) {
		if (httpMethod == null || !SUPPORTED_METHODS.contains(httpMethod) ||
				(noContentType && !message.hasBody())) {
			return null;
		}
		throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes);
	}

	MediaType selectedContentType = contentType;
	Object theBody = body;
	LogFormatUtils.traceDebug(logger, traceOn -> {
		String formatted = LogFormatUtils.formatValue(theBody, !traceOn);
		return "Read \"" + selectedContentType + "\" to [" + formatted + "]";
	});

	return body;
}
//我们基本大致看到一种参数解析器如何执行的 接下来看下方法2返回值处理器是如何执行的
//我们来到方法2
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
	//这里也是获取匹配的返回值处理器进行处理
    HandlerMethodReturnValueHandler handler = this.selectHandler(returnValue, returnType);s
    if (handler == null) {
        throw new IllegalArgumentException("Unknown return value type: " + returnType.getParameterType().getName());
    } else {
    	//我们看下这个方法 还是看我们平时标注了@ResponseBody注解的返回值处理器
        handler.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
    }
}

public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest)
		throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {

	mavContainer.setRequestHandled(true);
	ServletServerHttpRequest inputMessage = createInputMessage(webRequest);
	ServletServerHttpResponse outputMessage = createOutputMessage(webRequest);

	// Try even with null return value. ResponseBodyAdvice could get involved.
	writeWithMessageConverters(returnValue, returnType, inputMessage, outputMessage);
}

protected <T> void writeWithMessageConverters(@Nullable T value, MethodParameter returnType,
		ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage)
		throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {

	Object body;
	Class<?> valueType;
	Type targetType;

	if (value instanceof CharSequence) {
		body = value.toString();
		valueType = String.class;
		targetType = String.class;
	}
	else {
		body = value;
		valueType = getReturnValueType(body, returnType);
		targetType = GenericTypeResolver.resolveType(getGenericType(returnType), returnType.getContainingClass());
	}

	if (isResourceType(value, returnType)) {
		outputMessage.getHeaders().set(HttpHeaders.ACCEPT_RANGES, "bytes");
		if (value != null && inputMessage.getHeaders().getFirst(HttpHeaders.RANGE) != null &&
				outputMessage.getServletResponse().getStatus() == 200) {
			Resource resource = (Resource) value;
			try {
				List<HttpRange> httpRanges = inputMessage.getHeaders().getRange();
				outputMessage.getServletResponse().setStatus(HttpStatus.PARTIAL_CONTENT.value());
				body = HttpRange.toResourceRegions(httpRanges, resource);
				valueType = body.getClass();
				targetType = RESOURCE_REGION_LIST_TYPE;
			}
			catch (IllegalArgumentException ex) {
				outputMessage.getHeaders().set(HttpHeaders.CONTENT_RANGE, "bytes */" + resource.contentLength());
				outputMessage.getServletResponse().setStatus(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE.value());
			}
		}
	}
	//这里也是定义了媒体类型
	MediaType selectedMediaType = null;
	MediaType contentType = outputMessage.getHeaders().getContentType();
	boolean isContentTypePreset = contentType != null && contentType.isConcrete();
	if (isContentTypePreset) {
		if (logger.isDebugEnabled()) {
			logger.debug("Found 'Content-Type:" + contentType + "' in response");
		}
		selectedMediaType = contentType;
	}
	else {
		//这里我们看到显示查找了请求种支持的媒体类型
		HttpServletRequest request = inputMessage.getServletRequest();
		List<MediaType> acceptableTypes = getAcceptableMediaTypes(request);
		//这里是获取我们容器中支持返回结果的媒体类型 
		List<MediaType> producibleTypes = getProducibleMediaTypes(request, valueType, targetType);

		if (body != null && producibleTypes.isEmpty()) {
			throw new HttpMessageNotWritableException(
					"No converter found for return value of type: " + valueType);
		}
		List<MediaType> mediaTypesToUse = new ArrayList<>();
		//然后双循环 来进行匹配请求和返回的媒体类型 这就是内容协商
		for (MediaType requestedType : acceptableTypes) {
			for (MediaType producibleType : producibleTypes) {
				if (requestedType.isCompatibleWith(producibleType)) {
					mediaTypesToUse.add(getMostSpecificMediaType(requestedType, producibleType));
				}
			}
		}
		if (mediaTypesToUse.isEmpty()) {
			if (body != null) {
				throw new HttpMediaTypeNotAcceptableException(producibleTypes);
			}
			if (logger.isDebugEnabled()) {
				logger.debug("No match for " + acceptableTypes + ", supported: " + producibleTypes);
			}
			return;
		}
		//这里是对匹配的最媒体类型进行排序 浏览器在发送请求的时候 会支持多种媒体类型 每个类型会有权重 因此 会根据权重进行排序
		MediaType.sortBySpecificityAndQuality(mediaTypesToUse);
		//排序成功后选择最优的返回类型
		for (MediaType mediaType : mediaTypesToUse) {
			if (mediaType.isConcrete()) {
				selectedMediaType = mediaType;
				break;
			}
			else if (mediaType.isPresentIn(ALL_APPLICATION_MEDIA_TYPES)) {
				selectedMediaType = MediaType.APPLICATION_OCTET_STREAM;
				break;
			}
		}

		if (logger.isDebugEnabled()) {
			logger.debug("Using '" + selectedMediaType + "', given " +
					acceptableTypes + " and supported " + producibleTypes);
		}
	}
	//这里就是利用我们的消息转换器来转换我们的返回值 例如 将我们的返回值转换为json或者是xml格式 返回具体的格式 这个格式我们自己也可以自己定义格式 注册到容器中 然mvc匹配到我们自己定义的返回值处理器 
	if (selectedMediaType != null) {
		selectedMediaType = selectedMediaType.removeQualityValue();
		for (HttpMessageConverter<?> converter : this.messageConverters) {
			GenericHttpMessageConverter genericConverter = (converter instanceof GenericHttpMessageConverter ?
					(GenericHttpMessageConverter<?>) converter : null);
			if (genericConverter != null ?
					((GenericHttpMessageConverter) converter).canWrite(targetType, valueType, selectedMediaType) :
					converter.canWrite(valueType, selectedMediaType)) {
				body = getAdvice().beforeBodyWrite(body, returnType, selectedMediaType,
						(Class<? extends HttpMessageConverter<?>>) converter.getClass(),
						inputMessage, outputMessage);
				if (body != null) {
					Object theBody = body;
					LogFormatUtils.traceDebug(logger, traceOn ->
							"Writing [" + LogFormatUtils.formatValue(theBody, !traceOn) + "]");
					addContentDispositionHeader(inputMessage, outputMessage);
					if (genericConverter != null) {
						genericConverter.write(body, targetType, selectedMediaType, outputMessage);
					}
					else {
						((HttpMessageConverter) converter).write(body, selectedMediaType, outputMessage);
					}
				}
				else {
					if (logger.isDebugEnabled()) {
						logger.debug("Nothing to write: null body");
					}
				}
				return;
			}
		}
	}
	//最终我们转换完成后将结果处理后返回 封装到ModelAndView种返回给中央调度器
	if (body != null) {
		Set<MediaType> producibleMediaTypes =
				(Set<MediaType>) inputMessage.getServletRequest()
						.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);

		if (isContentTypePreset || !CollectionUtils.isEmpty(producibleMediaTypes)) {
			throw new HttpMessageNotWritableException(
					"No converter for [" + valueType + "] with preset Content-Type '" + contentType + "'");
		}
		throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes);
	}
}


这里我们大概可以总结一下:
mvc先是由匹配的适配器去先寻找合适的参数解析器,参数解析器有很多种,支持我们方法各种注解,利用参数解析器来转换我们传递的参数,有的参数解析器会用到参数转换器,然后根据传入的参数类型以及需要转换的参数类型来转换我们真正需要的类型以及结果,然后会将结果传入我们处理器的方法,执行完毕后返回结果,mvc再利用匹配到的返回值处理器进行转换,将返回的对象来转换成json/xml等格式返回给请求方


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值