SpringMVC源码(一)ContextLoaderListener
2017-06-05 18:20
337 查看
在我们启动 springweb 服务时,spring会初始化i
4000
oc容器(WebApplicationContext),装载我们所需要的bean。
具体配看我们的web.xml中配置。
看一下ContextLoaderListener 类,extends ContextLoader implements ServletContextListener
父类ContextLoader可以说是一个工具类,主要用来实现ContextLoaderListener中初始化ioc容器需要的方法。
即ContextLoaderListener 的initWebApplicationContext()方法在其父类中实现;
而实现的 ServletContextListener 类可以说是一个servlet 的监听器,当我们启动tomcat的时候,会默认初始化一些servlet,
当监听到servlet初始化时,ContextLoaderListener便会进行我们的初始化ioc容器。
即servlet 在初始化之后我们的ContextLoaderListener也会进行初始化。
我们主要看一下父类ContextLoader中的initWebApplicationContext() 方法,它将我们ioc(WebApplicationContext)
进行
初始化,加载spring文件里面的创建的bean实例,并将它放入到servletcontext当中去。看一下它的具体代码。
4000
oc容器(WebApplicationContext),装载我们所需要的bean。
具体配看我们的web.xml中配置。
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext*.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
看一下ContextLoaderListener 类,extends ContextLoader implements ServletContextListener
父类ContextLoader可以说是一个工具类,主要用来实现ContextLoaderListener中初始化ioc容器需要的方法。
即ContextLoaderListener 的initWebApplicationContext()方法在其父类中实现;
//初始化方法 public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); } //销毁方法 public void contextDestroyed(ServletContextEvent event) { closeWebApplicationContext(event.getServletContext()); ContextCleanupListener.cleanupAttributes(event.getServletContext()); }
而实现的 ServletContextListener 类可以说是一个servlet 的监听器,当我们启动tomcat的时候,会默认初始化一些servlet,
当监听到servlet初始化时,ContextLoaderListener便会进行我们的初始化ioc容器。
即servlet 在初始化之后我们的ContextLoaderListener也会进行初始化。
//servlet/jsp 容器会在应用程序初始化期间调用 contextInitialized()方法。 public void contextInitialized ( ServletContextEvent sce ); //销毁 public void contextDestroyed ( ServletContextEvent sce );
我们主要看一下父类ContextLoader中的initWebApplicationContext() 方法,它将我们ioc(WebApplicationContext)
进行
初始化,加载spring文件里面的创建的bean实例,并将它放入到servletcontext当中去。看一下它的具体代码。
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) { //判断是否创建过容器 if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) { throw new IllegalStateException( "Cannot initialize context because there is already a root application context present - " + "check whether you have multiple ContextLoader* definitions in your web.xml!"); } Log logger = LogFactory.getLog(ContextLoader.class); //打印日志 web容器开始初始化 servletContext.log("Initializing Spring root WebApplicationContext"); if (logger.isInfoEnabled()) { logger.info("Root WebApplicationContext: initialization started"); } long startTime = System.currentTimeMillis(); try { // Store context in local instance variable, to guarantee that // it is available on ServletContext shutdown. //创建WebApplicationContext if (this.context == null) { this.context = createWebApplicationContext(servletContext); } if (this.context instanceof ConfigurableWebApplicationContext) { ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context; //判断上下文 if (!cwac.isActive()) { // The context has not yet been refreshed -> provide services such as // setting the parent context, setting the application context id, etc //是否有父容器 if (cwac.getParent() == null) { // The context instance was injected without an explicit parent -> // determine parent for root web application context, if any. //获得父容器 ApplicationContext parent = loadParentContext(servletContext); //设置父容器 cwac.setParent(parent); } //刷新WebApplicationContext configureAndRefreshWebApplicationContext(cwac, servletContext); } } servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); ClassLoader ccl = Thread.currentThread().getContextClassLoader(); if (ccl == ContextLoader.class.getClassLoader()) { currentContext = this.context; } else if (ccl != null) { currentContextPerThread.put(ccl, this.context); } if (logger.isDebugEnabled()) { logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]"); } if (logger.isInfoEnabled()) { long elapsedTime = System.currentTimeMillis() - startTime; logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms"); } //返回这个容器 return this.context; } catch (RuntimeException ex) { logger.error("Context initialization failed", ex); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex); throw ex; } catch (Error err) { logger.error("Context initialization failed", err); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err); throw err; } }
相关文章推荐
- springMVC源码分析--容器初始化(一)ContextLoaderListener
- SpringMVC源码 1 ContextLoaderListener和Servlet容器web.xml配置
- SpringMVC源码阅读笔记-ContextLoaderListener
- springMVC源码分析--容器初始化(一)ContextLoaderListener
- springMVC源码分析--容器初始化(一)ContextLoaderListener
- springMVC源码分析--容器初始化(一)ContextLoaderListener
- web.xml中Spring使用的ContextLoaderListener和SpringMVC的DispatcherServlet之间的关系
- 共同学习Spring源码--ContextLoaderListener和ContextCleanupListener
- Dorado 7 源码分析(一)SpringContextLoaderListener
- Spring IOC学习心得之源码级分析ContextLoaderListener的作用(IOC容器初始化入口)
- SpringMVC容器初始化篇----ContextLoaderListener
- SpringMVC容器初始化篇----ContextLoaderListener
- springMVC中contextLoaderListener作用
- Spring源码学习之一 ContextLoaderListener及XmlWebApplicationContext
- Eclipse运行Maven的SpringMVC项目Run on Server时出现错误:Error configuring application listener of class org.springframework.web.context.ContextLoaderListener的问题解决
- 基于SpringMVC的web应用的ContextLoaderListener与DispatcherServlet标准组合配置
- Spring源码之ContextLoaderListener(1)
- 深挖SpringMVC_03_DispatcherServlet/ContextLoaderListener
- Spring源码之ContextLoaderListener(3)
- 【spring】源码分析 <一> 从ContextLoaderListener开始·