• SpringMVC执行流程


    SpringMVC常用组件

    DispatcherServlet:前端控制器,不需要工程师开发,由框架提供
    作用:统一处理请求和响应,整个流程控制的中心,由它调用其它组件处理用户的请求

    HandlerMapping:处理器映射器,不需要工程师开发,由框架提供

    作用:根据请求的url、method等信息查找Handler,即控制器方法
    Handler:处理器,需要工程师开发
    作用:在DispatcherServlet的控制下Handler对具体的用户请求进行处理

    HandlerAdapter:处理器适配器,不需要工程师开发,由框架提供
    作用:通过HandlerAdapter对处理器(控制器方法)进行执行

    ViewResolver:视图解析器,不需要工程师开发,由框架提供
    作用:进行视图解析,得到相应的视图,例如:ThymeleafView、InternalResourceView、

    RedirectView
    View:视图
    作用:将模型数据通过页面展示给用户

    DispatcherServlet初始化过程 

     DispatcherServlet 本质上是一个 Servlet,所以天然的遵循 Servlet 的生命周期。所以宏观上是 Servlet生命周期来进行调度。

     初始化WebApplicationContext

    所在类:org.springframework.web.servlet.FrameworkServle

    1. protected WebApplicationContext initWebApplicationContext() {
    2. WebApplicationContext rootContext =
    3. WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    4. WebApplicationContext wac = null;
    5. if (this.webApplicationContext != null) {
    6. // A context instance was injected at construction time -> use it
    7. wac = this.webApplicationContext;
    8. if (wac instanceof ConfigurableWebApplicationContext) {
    9. ConfigurableWebApplicationContext cwac =
    10. (ConfigurableWebApplicationContext) wac;
    11. if (!cwac.isActive()) {
    12. // The context has not yet been refreshed -> provide services
    13. such as
    14. // setting the parent context, setting the application context
    15. id, etc
    16. if (cwac.getParent() == null) {
    17. // The context instance was injected without an explicit
    18. parent -> set
    19. // the root application context (if any; may be null) as the
    20. parent
    21. cwac.setParent(rootContext);
    22. }
    23. configureAndRefreshWebApplicationContext(cwac);
    24. }
    25. }
    26. }
    27. if (wac == null) {
    28. // No context instance was injected at construction time -> see if one
    29. // has been registered in the servlet context. If one exists, it is
    30. assumed
    31. // that the parent context (if any) has already been set and that the
    32. // user has performed any initialization such as setting the context id
    33. wac = findWebApplicationContext();
    34. }
    35. if (wac == null) {
    36. // No context instance is defined for this servlet -> create a local one
    37. // 创建WebApplicationContext
    38. wac = createWebApplicationContext(rootContext);
    39. }
    40. if (!this.refreshEventReceived) {
    41. // Either the context is not a ConfigurableApplicationContext with
    42. refresh
    43. // support or the context injected at construction time had already been
    44. // refreshed -> trigger initial onRefresh manually here.
    45. synchronized (this.onRefreshMonitor) {
    46. // 刷新WebApplicationContext
    47. onRefresh(wac);
    48. }
    49. }
    50. if (this.publishContext) {
    51. // Publish the context as a servlet context attribute.
    52. // 将IOC容器在应用域共享
    53. String attrName = getServletContextAttributeName();
    54. getServletContext().setAttribute(attrName, wac);
    55. }
    56. return wac;
    57. }

     ②创建WebApplicationContext

    所在类:org.springframework.web.servlet.FrameworkServlet 

    1. protected WebApplicationContext createWebApplicationContext(@Nullable
    2. ApplicationContext parent) {
    3. Class contextClass = getContextClass();
    4. if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass))
    5. {
    6. throw new ApplicationContextException(
    7. "Fatal initialization error in servlet with name '" +
    8. getServletName() +
    9. "': custom WebApplicationContext class [" + contextClass.getName() +
    10. "] is not of type ConfigurableWebApplicationContext");
    11. }
    12. // 通过反射创建 IOC 容器对象
    13. ConfigurableWebApplicationContext wac =
    14. (ConfigurableWebApplicationContext)
    15. BeanUtils.instantiateClass(contextClass);
    16. wac.setEnvironment(getEnvironment());
    17. // 设置父容器
    18. wac.setParent(parent);
    19. String configLocation = getContextConfigLocation();
    20. if (configLocation != null) {
    21. wac.setConfigLocation(configLocation);
    22. }
    23. configureAndRefreshWebApplicationContext(wac);
    24. return wac;
    25. }


    ③DispatcherServlet初始化策略
    FrameworkServlet创建WebApplicationContext后,刷新容器,调用onRefresh(wac),此方法在DispatcherServlet中进行了重写,调用了initStrategies(context)方法,初始化策略,即初始化
    DispatcherServlet的各个组件
    所在类:org.springframework.web.servlet.DispatcherServlet

    1. protected void initStrategies(ApplicationContext context) {
    2. initMultipartResolver(context);
    3. initLocaleResolver(context);
    4. initThemeResolver(context);
    5. initHandlerMappings(context);
    6. initHandlerAdapters(context);
    7. initHandlerExceptionResolvers(context);
    8. initRequestToViewNameTranslator(context);
    9. initViewResolvers(context);
    10. initFlashMapManager(context);
    11. }

    SpringMVC的执行流程 

    用户向服务器发送请求,请求被SpringMVC 前端控制器 DispatcherServlet捕获。
    2) DispatcherServlet对请求URL进行解析,得到请求资源标识符(URI),判断请求URI对应的映射:
    a) 不存在
    i. 再判断是否配置了mvc:default-servlet-handler
    ii. 如果没配置,则控制台报映射查找不到,客户端展示404错误

    iii. 如果有配置,则访问目标资源(一般为静态资源,如:JS,CSS,HTML),找不到客户端也会展示404错误 

    b) 存在则执行下面的流程
    3) 根据该URI,调用HandlerMapping获得该Handler配置的所有相关的对象(包括Handler对象以及Handler对象对应的拦截器),最后以HandlerExecutionChain执行链对象的形式返回。
    4) DispatcherServlet 根据获得的Handler,选择一个合适的HandlerAdapter。
    5) 如果成功获得HandlerAdapter,此时将开始执行拦截器的preHandler(...)方法【正向】

    6) 提取Request中的模型数据,填充Handler入参,开始执行Handler(Controller)方法,处理请求。在填充Handler的入参过程中,根据你的配置,Spring将帮你做一些额外的工作:
    a) HttpMessageConveter: 将请求消息(如Json、xml等数据)转换成一个对象,将对象转换为指定的响应信息

    b) 数据转换:对请求消息进行数据转换。如String转换成Integer、Double等
    c) 数据格式化:对请求消息进行数据格式化。 如将字符串转换成格式化数字或格式化日期等
    d) 数据验证: 验证数据的有效性(长度、格式等),验证结果存储到BindingResult或Error中
    7) Handler执行完成后,向DispatcherServlet 返回一个ModelAndView对象。

    8) 此时将开始执行拦截器的postHandle(...)方法【逆向】。
    9) 根据返回的ModelAndView(此时会判断是否存在异常:如果存在异常,则执行
    HandlerExceptionResolver进行异常处理)选择一个适合的ViewResolver进行视图解析,根据Model
    和View,来渲染视图。
    10) 渲染视图完毕执行拦截器的afterCompletion(...)方法【逆向】。
    11) 将渲染结果返回给客户端。 

  • 相关阅读:
    Linux命令之shred命令
    数据采集平台(二)
    [附源码]Python计算机毕业设计Django天狗电子商城系统
    【思科设备命令最全合集,快快收藏!】
    一次吃亏的运维经历
    Halcon (5):Halcon Solution Guide I basics 导论解析
    Android SELinux
    使用ElementPlus实现内嵌表格和内嵌分页
    Threejs_04 gui调试开发
    归一化原来这么重要!深入浅出详解Transformer中的Normalization
  • 原文地址:https://blog.csdn.net/m0_62436868/article/details/126561938