`

springmvc执行过程

阅读更多



springmvc的执行过程:
  一个请求执行的是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<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("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);
				}
			}
		}
	}

   

   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.
				//找到Handler
				mappedHandler = getHandler(processedRequest, false);
				if (mappedHandler == null || mappedHandler.getHandler() == null) {
					noHandlerFound(processedRequest, response);
					return;
				}

				// Determine handler adapter for the current request.
				//找到通过Handler找到HandlerAdapter
				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.
				//执行handler
				mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

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

				applyDefaultViewName(request, mv);
				mappedHandler.applyPostHandle(processedRequest, response, mv);
			}
			catch (Exception ex) {
				dispatchException = ex;
			}
			processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
		}
		catch (Exception ex) {
			triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
		}
		catch (Error err) {
			triggerAfterCompletionWithError(processedRequest, response, mappedHandler, 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);
				}
			}
		}
	}



protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
                //handlerMappings配置的映射处理器
		for (HandlerMapping hm : this.handlerMappings) {
			if (logger.isTraceEnabled()) {
				logger.trace(
						"Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'");
			}
			
			HandlerExecutionChain handler = hm.getHandler(request);
			if (handler != null) {
				return handler;
			}
		}
		return null;
	}

public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
		//获取HandlerMethod。
		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 = getApplicationContext().getBean(handlerName);
		}
		//获取HandlerExecutionChain相当于加了spring拦截器的HandlerMethod。
		return getHandlerExecutionChain(handler, request);
	}


protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
                //获取uri
		String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
		if (logger.isDebugEnabled()) {
			logger.debug("Looking up handler method for path " + lookupPath);
		}
		//相当于找到controller中的方法
		HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);
		if (logger.isDebugEnabled()) {
			if (handlerMethod != null) {
				logger.debug("Returning handler method [" + handlerMethod + "]");
			}
			else {
				logger.debug("Did not find handler method for [" + lookupPath + "]");
			}
		}
		//通过beanname获取bean,这里也就是获取controller设置到handlerMethod中
		return (handlerMethod != null ? handlerMethod.createWithResolvedBean() : null);
	}

protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
		List<Match> matches = new ArrayList<Match>();
		//urlMap里面封装了以url为key的RequestMappingInfo
		List<T> directPathMatches = this.urlMap.get(lookupPath);
		if (directPathMatches != null) {
		        //将匹配到的加入List<Match>中
			addMatchingMappings(directPathMatches, matches, request);
		}
		if (matches.isEmpty()) {
			// No choice but to go through all mappings...
			addMatchingMappings(this.handlerMethods.keySet(), matches, request);
		}
                //寻找最合适的match
		if (!matches.isEmpty()) {
			Comparator<Match> comparator = new MatchComparator(getMappingComparator(request));
			Collections.sort(matches, comparator);
			if (logger.isTraceEnabled()) {
				logger.trace("Found " + matches.size() + " matching mapping(s) for [" + lookupPath + "] : " + matches);
			}
			Match bestMatch = matches.get(0);
			if (matches.size() > 1) {
				Match secondBestMatch = matches.get(1);
				if (comparator.compare(bestMatch, secondBestMatch) == 0) {
					Method m1 = bestMatch.handlerMethod.getMethod();
					Method m2 = secondBestMatch.handlerMethod.getMethod();
					throw new IllegalStateException(
							"Ambiguous handler methods mapped for HTTP path '" + request.getRequestURL() + "': {" +
							m1 + ", " + m2 + "}");
				}
			}
			handleMatch(bestMatch.mapping, lookupPath, request);
			return bestMatch.handlerMethod;
		}
		else {
			return handleNoMatch(handlerMethods.keySet(), lookupPath, request);
		}
	}



//总结:springmvc先根据我们的uri找到对应的Handler(相当于controller)。再加上配置的拦截器封装为HandlerExecutionChain执行链对象。
//然后根据handler找到合适的HandlerAdapter适配器。最后调用HandlerAdapter的方法执行返回ModelAndView。


  • 大小: 47.2 KB
分享到:
评论

相关推荐

    图解springMVC执行流程及原理.docx

    SpringMVC 的执行流程是理解其工作原理的关键,下面我们将详细解析这个过程。 首先,SpringMVC 的执行流程始于客户端发起的HTTP请求,这个请求被Web服务器接收到。在传统的Java Web应用中,Web服务器(如Tomcat)会...

    SpringMvc 执行流程

    SpringMvc 执行流程

    0从源码角度分析SpringMVC执行流程.java

    0从源码角度分析SpringMVC执行流程.java

    springmvc流程图

    springmvc执行流程图,图片下载,一看就会,面试必备

    SpringMVC执行流程简介1

    总结来说,SpringMVC 的执行流程主要包括:DispatcherServlet 的调度、HandlerMapping 的映射、HandlerAdapter 的适配、数据处理、Handler 的执行、ModelAndView 的返回、ViewResolver 的解析以及视图的渲染。...

    SpringMVC底层执行流程及原理解析

    五、SpringMVC执行流程 下面是SpringMVC的执行流程: 1. DispatcherServlet接收到请求 2. HandlerMapper根据请求的地址去找处理器 3. HandlerAdapter找到处理器后根据id去适配对应的controller 4. controller处理...

    springmvc 的工作流程.docx

    ### SpringMVC的工作流程详解 #### 一、概述 SpringMVC是Spring框架的一个模块,主要用于简化Web应用程序的开发过程。它遵循Model-View-Controller(MVC)设计模式,帮助开发者更好地组织代码结构,提高代码的可...

    springMVC使用文档

    SpringMVC 的执行流程如下: 1. **请求处理**:用户发起 HTTP 请求至 DispatcherServlet。 2. **请求分发**:DispatcherServlet 负责根据配置规则将请求分发到相应的 Controller。 3. **处理请求**:Controller ...

    SpringMVC的执行流程及组件详解

    SpringMVC的执行流程及组件详解 SpringMVC是一个基于Java的Web应用程序框架,它提供了一种灵活的方式来构建Web应用程序。理解SpringMVC的执行流程及组件是开发高质量Web应用程序的关键。本文将对SpringMVC的执行...

    SpringMVC的执行过程浅析

    以下是对SpringMVC执行过程的详细分析: 1. **请求接收**:当用户发起一个HTTP请求,如`http://localhost:9999/SpringMVC/input-product`,这个请求首先会被部署在服务器上的SpringMVC DispatcherServlet捕获。...

    spring执行流程图

    流程图,讲述了springMVC执行流程

    SpringMvc主要流程源码解析(1).zip

    在 Controller 方法执行过程中,可能会涉及到模型数据的绑定。Spring MVC 提供了 DataBinder,它可以将请求参数自动绑定到 Controller 方法的参数上,也可以将 Model 数据绑定到视图中。 6. **视图解析**: 当 ...

    SpringMVC讲义.docx

    **SpringMVC执行流程** 1. **HTTP请求捕获**:用户向服务器发送HTTP请求,被前端控制器DispatcherServlet捕获。 2. **URL解析与HandlerMapping**:DispatcherServlet解析请求的URL,获取请求资源标识符(URI),并...

    SpringMVC 处置流程分析

    3. 请求处理流程一旦请求到达,SpringMVC的处置流程如下: 3.1 请求被DispatcherServlet捕获。 3.2 根据请求URL,DispatcherServlet查找HandlerMapping,找到合适的处理器(Controller)。 3.3 HandlerAdapter...

    qfedu三阶段SpringMVC

    #### 三、SpringMVC执行流程 SpringMVC的执行流程主要包括以下几个步骤: 1. 用户发送HTTP请求至前端控制器(`DispatcherServlet`)。 2. `DispatcherServlet`根据配置文件中的处理器映射器(`HandlerMapping`)...

    SpringMVC入门级教程,免费下载

    通过了解和掌握 SpringMVC 的基本概念、架构以及执行流程,你将能更好地利用这个框架构建高性能、易于维护的 Java Web 应用。对于初学者来说,跟随这个入门级教程,一步步实践项目搭建,将有助于快速上手 SpringMVC...

    java的ssm框架中的SpringMVC的学习笔记课件

    【SpringMVC 执行流程】 1. 用户发送 HTTP 请求,被前端控制器 DispatcherServlet 捕获。 2. DispatcherServlet 解析请求 URL,通过 HandlerMapping 获取对应 Handler 及其相关的拦截器。 3. 选择合适的 ...

    SpringMVC深入总结 核心流程图 中关村北大青鸟网上学堂 docx

    如上所述,SpringMVC的核心流程图主要展示了从用户请求到达服务器到最终响应给用户的过程。下面是对该流程图的详细解读: 1. **初始化阶段**:当应用启动时,`DispatcherServlet`读取配置文件并初始化相应的组件,...

    springMVC框架学习

    #### 四、SpringMVC执行流程 SpringMVC遵循MVC设计模式的基本原则,其执行流程如下: 1. 用户发送HTTP请求到前端控制器DispatcherServlet。 2. DispatcherServlet查询HandlerMapping,找到相应的Handler...

Global site tag (gtag.js) - Google Analytics