Source code reading - the process of parsing SpringMVC through debug source code

Prepare

Simply build a Spring MVC project, write a Controller, and run a basic request forwarding

DispatcherServlet class diagram analysis

DispatcherServlet.class
From this figure, we can analyze that the FrameworkServlet inherited by DispatcherServlet implements the ApplicationContextAware interface in Spring, and then indirectly inherits HttpServlet. So it is essentially a Servlet, (because the entry point of Spring MVC is Servlet, which is different from Struts2, which is Filter). By implementing ApplicationContextAware, you can get the Spring container, and you can get the @Controller-annotated classes in the Spring container.

breakpoint debugging

Breakpoint debugging DispatcherServlet.class->onRefresh()->initStrategies(), look at the initialization process of Spring MVC.

  • The first process
    First, Spring has loaded the beans we annotated or configured in xml.
    At this stage, DispatcherServlet will take out beans from the Spring container to initialize HandlerMapping, HandlerAdapter, ViewResolver, etc. and put them into the DispatcherServlet object. Breakpoint init* method to see its process.
    The role of HandlerMapping is to find the corresponding handler according to the request. The role of Handler
    Adapter is to call specific methods to process the request sent by the user.
    The role of ViewResolver is to resolve the logical view name returned by Handler into a real View View object.
  • The second process:
    request mapping and packaging Return:
    In web.xml, the url of the servlet-mapping configuration servlet is /, then all requests will enter this DispatcherServlet.
    In FrameworkServlet, the service method is rewritten, of course, here is just some judgment.
    (The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.), and calls doGet, doPost, doPut, doDelete, etc. when appropriate.)
    After the request is entered, the one-time entry method:
    service()->enter Corresponding doGet or doPost, etc.->processRequest()->doService()->doDispatch()
    The doDispatch method is the core of the mapping, in which it will get the mappedHandler corresponding to the request address, so as to get the corresponding HandlerAdapter, and then HandlerAdapter executes the handle method (which can be understood as executing the method in the corresponding Controller by reflection) and returns ModelAndView.
    The last is the process of view resolution.

Finishing process:

  • The user sends a request to the front controller DispatcherServlet.
  • The DispatcherServlet receives the request and calls the HandlerMapping handler mapper.
  • The processor mapper finds a specific processor (which can be searched according to xml configuration and annotations), generates a processor object and a processor interceptor (if any) and returns it to DispatcherServlet.
  • DispatcherServlet calls HandlerAdapter handler adapter.
  • HandlerAdapter is adapted to call a specific handler (Controller, also called a back-end controller).
  • Controller execution completes and returns to ModelAndView.
  • HandlerAdapter returns the controller execution result ModelAndView to DispatcherServlet.
  • DispatcherServlet passes ModelAndView to ViewReslover view resolver.
  • ViewReslover returns a specific View after parsing.
  • DispatcherServlet renders the view according to the View (that is, fills the model data into the view).
  • The DispatcherServlet responds to the user.
    SpringMVC flow chart

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326107821&siteId=291194637