java源码-springmvc

<img

https://www.cnblogs.com/Java-Starter/p/10310565.html

初始化过程

https://blog.csdn.net/dhaiuda/article/details/80026354

总结下HttpServletBean,FrameworkServlet和DispatcherServlet初始化过程

1.HttpServletBean

初始化web.xml中的参数

2.FrameworkServlet

将上下文赋予当前Servlet

3.DispatcherServlet

初始化HandlerMapping(请求映射),HandlerExceptionResolver(异常处理),ViewResolver(视图解析)等功能实现类

img

DispatcherServlet处理请求

在浏览器输入http://localhost:8080/springmvcdemo/employee,触发DispatcherServlet的processRequest方法

先看父类FrameworkServlet的processRequest方法:

previousLocaleContext获取和当前线程相关的LocaleContext

根据已有请求构造一个新的和当前线程相关的LocaleContext

previousAttributes获取和当前线程绑定的RequestAttributes

为已有请求构造新的ServletRequestAttributes,加入预绑定属性

initContextHolders让新构造的RequestAttributes和ServletRequestAttributes和当前线程绑定,加入到ThreadLocal,完成绑定

抽象方法doService由FrameworkServlet子类DispatcherServlet重写

img

img

resetContextHolders方法解除RequestAttributes,ServletRequestAttributes和当前线程的绑定

注册监听事件ServletRequestHandledEvent,在调用上下文的时候产生Event

现在我们看下DispatcherServlet的doService方法

img

attributesSnapshot用来保存request域中的数据,可以叫做“快照”

img

进入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);

				
                //通过当前request获取到 HandlerExecutionChain
				mappedHandler = getHandler(processedRequest);
				if (mappedHandler == null) {
					noHandlerFound(processedRequest, response);
					return;
				}

				
                //确定当前请求的处理程序适配器。
				HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

				// 如果处理程序支持,则处理最后修改的标头
				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;
					}
				}

				if (!mappedHandler.applyPreHandle(processedRequest, response)) {
					return;
				}

				// 实际调用处理程序。
				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);
				}
			}
		}
	}
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");
}

第一个就是:从2个handler中,找合适的控制器

BeanNameUrlHandlerMapping@833153a, XML的配置方式控制器

RequestMappingHandlerMapping@3602f818 注解方式的控制器

第二个就是:从3个Adapter中,找合适的处理器

HttpRequestHandlerAdapter@28dc72d7, SimpleControllerHandlerAdapter@6b712691, XML的配置方式的Adapter、annotation

RequestMappingHandlerAdapter@4d763f47 注解方式的Adapter

https://blog.csdn.net/abcwanglinyong/article/details/91044492

img

1、用户发送请求,被 SpringMVC 的前端控制器 DispatcherServlet 拦截。

2、DispatcherServlet 收到请求后调用 HandlerMapping 处理器映射器。

3、处理器映射器找到具体的处理器,生成处理器执行链HandlerExecutionChain(包含处理器Handler和处理器拦截器集合),返回给 DispatcherServlet。

4、DispatcherServlet 调用 HandlerAdapter 处理器适配器。

5、通过HandlerAdapter处理器适配器找到具体的Controller

6、Controller将模型数据Model封装到 ModelAndView 返回给 HandlerAdapter。

7、HandlerAdapter将模型视图ModelAndView 返回给 DispatcherServlet。

8、DispatcherServlet 将 ModelAndView 传给 ViewReslover 视图解析器。

9、ViewReslover 解析后返回具体的 View。

10、DispatcherServlet 根据 View 进行视图渲染(即将模型数据填充至视图中)。

11、DispatcherServlet 将视图响应给用户。
给 DispatcherServlet。

8、DispatcherServlet 将 ModelAndView 传给 ViewReslover 视图解析器。

9、ViewReslover 解析后返回具体的 View。

10、DispatcherServlet 根据 View 进行视图渲染(即将模型数据填充至视图中)。

11、DispatcherServlet 将视图响应给用户。

发布了37 篇原创文章 · 获赞 6 · 访问量 4651

猜你喜欢

转载自blog.csdn.net/littlewhitevg/article/details/105475071