Alice52 / spring-5.2.x

spring source code
https://github.com/Alice52/spring-5.2.x/issues/2
0 stars 1 forks source link

[mvc] sequence of component #16

Closed Alice52 closed 2 years ago

Alice52 commented 4 years ago

load

  1. load web.xml, pasre and tag
  2. create ServletContext
  3. convert and ste to ServletContext
  4. create instance

execute

  1. Listener

  2. Filter: execute by define order

  3. Interceptor: HandlerInterceptor / HandlerInterceptorAdapter

    • preHandle: Sequential call
    • postHandler: Call in reverse order + postHandler called when all interceptors in the interceptor chain execute successfully
    • afterCompletion: Call in reverse order + if preHandle, must afterCompletion
    // First & Second 都放行
    MyFirstInterceptor..preHandle...
    MySecondInterceptor..preHandle...
    handle01处理方法...
    MySecondInterceptor..postHandle...
    MyFirstInterceptor..postHandle...
    jsp页面.......
    MySecondInterceptor..afterCompletion...
    MyFirstInterceptor..afterCompletion...
    
    // First 放行 & Second-pre 不放行
    MyFirstInterceptor..preHandle...
    MyFirstInterceptor..afterCompletion...
  4. make two Interceptor execute sequential:

    • extends WebMvcConfigurerAdapter or implement WebMvcConfigurer,
    • then overwrite or implement addInterceptor() method
  5. Servlet[DispatcherServlet]

  6. postHandler

Alice52 commented 4 years ago

mvc request flow

diagram

Selection_122

explain

  1. request --> DispatcherServlet
  2. DispatcherServlet --> xxHandlerMapping[Adaptor]: 确定请求被路由到那个 handler
  3. HandlerExecutionChain: 执行链
  4. +5
  5. HandlerAdatpor 执行 执行链 上的handler: controller 的逻辑再此处执行
  6. ModelAndView: 返回 controller处理之后的 ModelAndView
  7. +8
  8. +9
  9. DispatcherServlet得到 ModelAndView, 并使用 View Resolver处理返回 View
  10. DispatcherServlet根据得到的 view 进行视图渲染, 并且将 数据 Model 填充到 request 域中
  11. response
Alice52 commented 4 years ago

1-5 detail

  1. DispatcherServlet: doDispach
  2. RequestMappingHandlerAdaptor: handleInternal + invokeHandlerMethod + invokeAndHandle
  3. ServletInvocableHandlerMethod: invokeForRequest
  4. InvocableHandlerMethod: resolveArgument
  5. list-detail
    • ModelAttributeMethodProcessor: validateIfApplicable
    • RequestResponseBodyMethodProcessor: validateIfApplicable
    • RequestResponseBodyArgumentResolver: validateIfApplicable
Alice52 commented 4 years ago

mvc emmbed tomcat flow

  1. web container auto config theory

    • tomcat
    • jettty: long socket
    • undertom: concurrent
    <!-- dependency -->
    <!-- remove default web container: tomcat -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
       <exclusion>
           <artifactId>spring-boot-starter-tomcat</artifactId>
           <groupId>org.springframework.boot</groupId>
       </exclusion>
    </exclusions>
    </dependency>
    
    <!-- import new web container -->
    <dependency>
    <artifactId>spring-boot-starter-jetty</artifactId>
    <groupId>org.springframework.boot</groupId>
    </dependency>
    • auto config

      ServletWebServerFactoryConfiguration ServletWebServerFactoryAutoConfiguration: BeanPostProcessorsRegistrar EmbeddedWebServerFactoryCustomizerAutoConfiguration

    • step

    1. SpringBoot will inject ServletWebServerFactoryConfiguration to create xxFactory when necessary
    2. when container create webContainer will call BeanPostProcessorsRegistrar to exectue customize() method

link

  1. https://www.jianshu.com/p/6d6d2e47bf41
Alice52 commented 3 years ago

tip of mvc

  1. reference