Java: Spring MVC请求处理流程

类的继承关系

  1. public class DispatcherServlet extends FrameworkServlet
  2. public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware
  3. public abstract class HttpServletBean extends HttpServlet implements EnvironmentCapable, EnvironmentAware
  4. public abstract class javax.servlet.http.HttpServlet extends GenericServlet
  5. public abstract class javax.servlet.GenericServlet implements Servlet, ServletConfig,java.io.Serializable
  6. public interface javax.servlet.Servlet

代码处理流程

  1. DispatcherServlet.java: doService()开始处理请求;
  2. DispatcherServlet.java: getHandler() 遍历几种handler
  3. AbstractHandlerMapping.java: getHandler() 获取处理类的实例
  4. AbstractHandlerMapping.java: getHandler() 检查是否为跨域请求(检查代码: request.getHeader(“Origin”) != null;)
  5. AbstractHandlerMethodMapping.java: lookupHandlerMethod()->实际是在addMatchingMappings()方法里获取请求url对应的Controller全限定类路径及返回参数及全限定方法名.
    (例如(因代码过长,所以折行.):
    public 
                   java.util.List     
                            xyz.suancaiyu.complexcontrollerdemo.controller.CoffeeController.getAll()
    )
    
  6. DispatcherServlet.java: doService()实际调用处理程序:mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
  7. DispatcherServlet.java: doService()处理完成,并返回.

实际调用链路

Spring MVC请求调用链路
image-2760

其他

@RequestMapping注解的处理类: RequestMappingHandlerAdapter

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据