SpringMVC @RequestMapping 调用

DiapatcherServlet的继承树SpringDiapatcherServlet的继承树

执行调用
在这里插入图片描述

org.springframework.web.servlet.FrameworkServlet.processRequest(HttpServletRequest, HttpServletResponse)

/**
	 * Process this request, publishing an event regardless of the outcome.   处理此请求,发布事件,而不管结果如何。
	 * <p>The actual event handling is performed by the abstract    <p> 实际的事件处理是由抽象的模板方法执行的
	 * {@link #doService} template method.
	 */
	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(request, response);     //  在DispatcherServlet 中实现  然后调用 doDispatcherServlet 方法
		}
		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);
		}
	}

org.springframework.web.servlet.DispatcherServlet.doDispatch(HttpServletRequest,HttpServletResponse)

/**
	 * 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 {
   
			// checkMultipart 的作用:Convert the request into a multipart request, and make multipart resolver available. If no multipart resolver is set, simply use the existing request.
			// 将请求转换为多部分请求,并使多部分解析器可用。 如果没有设置多部分解析器,使用现有的请求。
			
				processedRequest = checkMultipart(request);
				multipartRequestParsed = (processedRequest != request);
				
				// 确定当前请求的handle 和 handler的执行链
				// Determine handler for the current request.
				mappedHandler = getHandler(processedRequest);
				// mappedHandler 中的参数在图1.2中
	|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
	获取mappedHandler
> /**
	 * Return the HandlerExecutionChain for this request.
	 * <p>Tries all handler mappings in order.
	 * @param request current HTTP request
	 * @return the HandlerExecutionChain, or {@code null} if no handler could be found
	 */
	@Nullable
	protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
   
		// handlermappings是springmvc初始化后得出来的
		// 相关解释下图 1.1
		if (this.handlerMappings != null) {
   
			for (HandlerMapping mapping : this.handlerMappings) {
   
				HandlerExecutionChain handler = mapping.getHandler(request);
				if (handler != null) {
   
					return handler;
				}
			}
		}
		return null;
	}
	|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||



				if (mappedHandler == null || mappedHandler.getHandler() == null) {
   
					// 没有找到处理请求的handle 去注释和报错 NoHandlerFoundException
					noHandlerFound(processedRequest, response);
					return;
				}

				// 确定在handle中该请求对应的处理器   可以解析对应请求的参数   和 相应数据
				// Determine handler adapter for the current request.
				HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

	|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/**
	 * Return the HandlerAdapter for this handler object.
	 * @param handler the handler object to find an adap
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值