SpringMVC DispatcherServlet 初始化过程
2016-03-31 16:35
337 查看
DispatcherServlet extends FrameworkServlet extends HttpServletBean extends HttpServlet
初始化:HttpServletBean.init() -> FrameworkServlet.initServletBean -> FrameworkServlet.initWebApplicationContext()
initWebApplicationContext:
1. 调用WebApplicationContextUtils.getWebApplicationContext(ServletContext)获取当前web容器中的根WebApplicationContext rootContext,该rootContext通常由ContextLoaderListener生成(因为该方法实际是获取web容器中attrName为org.springframework.web.context.WebApplicationContext.ROOT的WebApplicationContext,而该attrName由ContextLoaderListener设置到web容器中)
2. 判断本Servlet所属的WebApplicationContext wac是否在init之前就由别的步骤注入了?即判断wac是否为null
2.1. 若非空(wac已存在),则将rootContext设为wac的parent,再调用configureAndRefreshWebApplicationContext(wac)
2.2. 若为空(wac不存在),则先调用findWebApplicationContext寻找web容器中是否已经存在同名(attrName,定义见5)的WebApplicationContext(首次启动web容器时通常不存在;当servlet初始化失败,web容器自动重启时可能存在),若还是不存在则调用createWebApplicationContext生成一个新的WebApplicationContext
2.2.1. createWebApplicationContext(rootContext): 首先创建一个全新的WebApplicationContext wac,再将rootContext设为wac的parent,再调用configureAndRefreshWebApplicationContext(wac)
3. configureAndRefreshWebApplicationContext(wac):
先设置wac的各项配置和环境参数,后调用wac.refresh()
4. DispatcherServlet.onRefresh(wac): 初始化与web mvc相关的spring组件
5. 调用ServletContext.setAttribute将wac注册到web容器中,attrName为org.springframework.web.servlet.FrameworkServlet.CONTEXT.${servlet-name}
初始化:HttpServletBean.init() -> FrameworkServlet.initServletBean -> FrameworkServlet.initWebApplicationContext()
initWebApplicationContext:
1. 调用WebApplicationContextUtils.getWebApplicationContext(ServletContext)获取当前web容器中的根WebApplicationContext rootContext,该rootContext通常由ContextLoaderListener生成(因为该方法实际是获取web容器中attrName为org.springframework.web.context.WebApplicationContext.ROOT的WebApplicationContext,而该attrName由ContextLoaderListener设置到web容器中)
2. 判断本Servlet所属的WebApplicationContext wac是否在init之前就由别的步骤注入了?即判断wac是否为null
2.1. 若非空(wac已存在),则将rootContext设为wac的parent,再调用configureAndRefreshWebApplicationContext(wac)
2.2. 若为空(wac不存在),则先调用findWebApplicationContext寻找web容器中是否已经存在同名(attrName,定义见5)的WebApplicationContext(首次启动web容器时通常不存在;当servlet初始化失败,web容器自动重启时可能存在),若还是不存在则调用createWebApplicationContext生成一个新的WebApplicationContext
2.2.1. createWebApplicationContext(rootContext): 首先创建一个全新的WebApplicationContext wac,再将rootContext设为wac的parent,再调用configureAndRefreshWebApplicationContext(wac)
3. configureAndRefreshWebApplicationContext(wac):
先设置wac的各项配置和环境参数,后调用wac.refresh()
4. DispatcherServlet.onRefresh(wac): 初始化与web mvc相关的spring组件
5. 调用ServletContext.setAttribute将wac注册到web容器中,attrName为org.springframework.web.servlet.FrameworkServlet.CONTEXT.${servlet-name}
相关文章推荐
- java后台通过url获取返回数据
- java#2servlet
- Java 四种线程池的用法分析
- Struts2 传递参数map对象
- 华为上机题-二师弟的星际加法
- JavaEE_3_30_servlet详细介绍
- Swagger与SpringMVC项目整合
- maven eclipse 安装
- java传递String参数
- Spring IoC
- 用Java语言打印九九乘法口诀表
- spring+ibatis事务配置
- 30、java中递归算法
- Java - Collection
- JavaSe基础(18)-- 嵌套循环 案例
- java bridge
- java多线程细微知识点
- 华为上机题-排号机
- java程序性能优化
- Java实验1 java语言程序设计基础训练