美文网首页
Spring MVC运行机制的简单了解

Spring MVC运行机制的简单了解

作者: 我不懂我不懂a | 来源:发表于2021-02-21 16:14 被阅读0次

Spring 加载

  1. web.xml进行context-param和listener配置。
  2. servlet容器(tomcat)会读取context-param 的配置转为 key-value 存入ServletContext中,可通过event.servletContext.getInitParamter("contextConfigLocation") 获取。
  3. servlet容器启动会触发ServletContextListener的contextInitialized(ServletContextEvent)方法,spring mvc需要配置其自定义的实现ContextLoaderListener
  4. listener创建WebApplicationContext 并加载contextConfigLocation配置,然后刷新容器。
  5. 在 ContextLoader(ContextLoaderListener父类)中的initWebApplicationContext(ServletContext)方法中,servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); 把webApplicationContext记录在ServletContext可以方便全局调用。
  6. GenericServlet接口提供了getServletContext() 方法可以调用ServletContext实例。传统web开发都会使用到HttpServlet,就是集成了GenericServlet。

DispatcherServlet

相关文章

网友评论

      本文标题:Spring MVC运行机制的简单了解

      本文链接:https://www.haomeiwen.com/subject/vjbtfltx.html