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