Spring MVC运行机制的简单了解

2021-02-21  本文已影响0人  我不懂我不懂a

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

上一篇 下一篇

猜你喜欢

热点阅读