Spring mvc运行流程

原创 2018年04月15日 21:50:00

总体运行流程如下:

Spring mvc 执行入口为:DispatcherServlet;需要在web.xml中配置如下

<web-app>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/app-context.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>app</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>app</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>

</web-app>

在请求到DispatcherServelt后,请求将会执行其中的doService方法,doService被调用的方式如下:

doService中

	@Override
	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<String, Object> attributesSnapshot = null;
		if (WebUtils.isIncludeRequest(request)) {
			attributesSnapshot = new HashMap<String, Object>();
			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());

		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方法,这个方法里会进行请求的分发,及处理。

/**
	 * Process the actual dispatching to the handler.
	 * <p>The handler will be obtained by applying the servlet's HandlerMappings in order.
	 * The HandlerAdapter will be obtained by querying the servlet's installed HandlerAdapters
	 * to find the first that supports the handler class.
	 * <p>All HTTP methods are handled by this method. It's up to HandlerAdapters or handlers
	 * themselves to decide which methods are acceptable.
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @throws Exception in case of any kind of processing failure
	 */
	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);
                                 //获取那个请求中处理器去处理URL
				// 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;
					}
				}
                                  //执行preHandle
				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);
			}
			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);
			}//分发请求结果
			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.https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-servlet

SpringMVC - 运行流程图及原理分析

转自:http://blog.csdn.net/j080624/article/details/56273665 Spring MVC工作流程图 图一 图二  图...
  • xiaoxufox
  • xiaoxufox
  • 2017-08-15 16:51:33
  • 2984

springmvc的执行流程详解

1.什么是MVC MVC是Model View Controller的缩写,它是一个设计模式  2.springmvc执行流程详细介绍   第一步:发起请求到前端控制器(...
  • u014010769
  • u014010769
  • 2015-08-08 07:24:26
  • 27695

Spring MVC执行流程

1.1 架构图1.2 执行流程1.用户发送请求至前端控制器DispatcherServlet 2.DispatcherServlet收到请求调用处理器映射器HandlerMapping。 3.处理...
  • dfb198998
  • dfb198998
  • 2017-02-16 19:48:24
  • 1534

Spring MVC 流程图

Spring MVC工作流程图 图一 图二  Spring工作流程描述       1. 用户向服务器发送请求,请求...
  • zuoluoboy
  • zuoluoboy
  • 2014-02-23 19:49:35
  • 105758

springMVC执行流程及原理

spring的MVC执行原理 1.spring mvc将所有的请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责对请求 进行真正的处理工作。 2.Dispatcher...
  • liangzi_lucky
  • liangzi_lucky
  • 2016-09-07 14:34:34
  • 36220

springMVC运行流程分析

在整个 Spring MVC 框架中, DispatcherServlet 处于核心位置,负责协调和组织不 同组件以完成请求处理并返回响应的工作 。SpringMVC 处理请求过程: 1). 若一个...
  • u013628152
  • u013628152
  • 2016-05-17 23:06:13
  • 2764

用于理解Spring+SpringMVC+Hibernate开发流程的例子

  • 2017年06月04日 17:15
  • 14.92MB
  • 下载

SpringMVC执行的流程

SpringMVC执行流程,其实也就是请求—>响应的一个完整流程 按照图中描述的,SpringMVC请求—>响应的完整工作流程如下: 序号1:用户向服务器发送请求,请求被SpringMVC的前端控...
  • zouxucong
  • zouxucong
  • 2017-03-16 15:49:20
  • 1554

springMVC执行流程及环境搭建

springMVC几大核心组件       1. DispatcherServlet :前端控制器,将请求分派到具体的控制器(controller)        2. Controller :完成业...
  • liusong0605
  • liusong0605
  • 2013-08-19 10:04:21
  • 2911

springmvc----源码分析之springmvc执行流程

点滴记载,点滴进步,愿自己更上一层楼。
  • xu1916659422
  • xu1916659422
  • 2017-08-22 20:35:07
  • 783
收藏助手
不良信息举报
您举报文章:Spring mvc运行流程
举报原因:
原因补充:

(最多只允许输入30个字)