Spring原码解析—SpringMVC请求处理流程

我们都知道SpringMVC的核心就是DispatcherServlet,它负责接收请求和调度,在开始原码解析前先来看看SpringMVC请求的一个处理流程图:

1)客户端发送请求,DispatcherServlet接收请求

2)DispatcherServlet将请求交给DefaultAnnotationHandlerMapping找到对应的映射方法

3)通过对应的DefaultAnnotationHandlerAdapter  invoke特定的方法

4)调用自己的Controller和相应的方法,给Model设置数据和返回视图

5)DefaultAnnotationHandlerAdapter组装ModelAndView并返回给DispatcherServlet

6)通过InternalResourceViewResolver解析

7)响应客户端


对请求流程有了一个初步的了解后,接下来我们开始原码分析:

确定请求入口:

因为DispatcherServlet本质上是一个Servlet所有它一定有doPost()和doGet()方法,而DispatcherServlet又继承FrameworkServlet,通过分析我们在FrameworkServlet中找到请求处理方法,如图:

而doPost()和doGet()方法又会调用pocessRequest()这个方法,如图:

            

因此我们在processRequest这个方法上打上断点。

接下来开启debug并发送请求,来到断点处processRequest方法,其原码如下:


	//处理请求
	protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		long startTime = System.currentTimeMillis();
		Throwable failureCause = null;
		//获取LocaleContextHolder中原来保存的LocaleContext
		//刚开始一般是null
		LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
		//获取当前请求的LocaleContext
		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());
   //将当前请求的LocaleContext和ServletRequestAttributes设
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wonder4work

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值