Spring启动篇

来源:互联网 发布:中国气象数据网账号 编辑:程序博客网 时间:2024/06/15 19:51

Spring启动

Spring 中 BeanFactory一般以编程的方式启动,当然ApplicationContext也支持,但其更多的友好支持声明式创建。

容器启动过程可简单理解为:1预加载配置信息,2初始化相关类;一般他们通过Servlet或监听器来完成启动触发。

在Spring中,主要有ContextLoaderListener,ContextLoaderServlet来初始化Spring容器。

ContextLoaderListener继承自ContextLoader同时实现ServletContextListener,可以看下其说明:

Bootstrap listener to start up and shutdown Spring's root {@link WebApplicationContext}.

Simply delegates to {@link ContextLoader}as well as to {@link ContextCleanupListener}.

This listener should be registered after{@link org.springframework.web.util.Log4jConfigListener} in web.xml, if thelatter is used.

当通过ContextLoaderListener启动时,需要在web.xml添加如下配置。

<listener>      <listener-class>  org.springframework.web.context.ContextLoaderListener  </listener-class>  <listener>


其与web服务器的生命周期紧密相关,web服务器启动时,服务器声明周期事件触发ContextLoaderListener加载配置文件,启动初始化。其中重要的方法:contextInitialized(),contextDestoryed(),一个用于启动,一个用于销毁。




可以查看下ContextLoader的initWebApplicationContext源码


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);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.if (this.context == null) {this.context = createWebApplicationContext(servletContext);}if (this.context instanceof ConfigurableWebApplicationContext) {configureAndRefreshWebApplicationContext((ConfigurableWebApplicationContext)this.context, 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;}}




原创粉丝点击