Web容器启动时加载Spring分析

来源:互联网 发布:南京凶宅数据库 编辑:程序博客网 时间:2024/06/08 18:34

在应用程序web.xml中做了以下配置信息时,当启动Web容器时就会自动加载Spring容器。

[java] view plain copy
 print?
  1. <listener>  
  2.   
  3.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  4.   
  5. </listener>  

ContextLoaderListener类实现了javax.servlet.ServletContextListener接口并且继承了org.springframework.web.context.ContextLoader类。ServletContextListener事件类是Web容器的一部分,处理Web应用的Servlet上下文(context)的监听。实现ServletContextListener接口中的contextInitialized和contextDestroyed方法。当Web容器启动时会自动调用contextInitialized方法,进行初始化Spring Web应用程序上下文,主要加载web.xml中contextConfigLocation的配置文件;当Web容器关闭之前会调用contextDestroyed方法,进行销毁Spring Web应用程序上下文。ContextLoader类实现了Spring上下文初始化的工作,执行initWebApplicationContext方法返回WebApplicationContext。Spring实现的contextInitialized和contextDestroyed代码如下:
[java] view plain copy
 print?
  1. public void contextInitialized(ServletContextEvent event) {  
  2.   
  3.        this.contextLoader = createContextLoader();  
  4.   
  5.        if (this.contextLoader ==null) {  
  6.   
  7.            this.contextLoader = this;  
  8.   
  9.        }  
  10.   
  11.     this.contextLoader.initWebApplicationContext(event.getServletContext());  
  12.   
  13.     }  
  14.   
  15.    
  16.   
  17. public void contextDestroyed(ServletContextEvent event) {  
  18.   
  19.        if (this.contextLoader !=null) {        
[java] view plain copy
 print?
  1. this.contextLoader.closeWebApplicationContext(event.getServletContext());  
[java] view plain copy
 print?
  1. }  
  2.   
  3.  ContextCleanupListener.cleanupAttributes(event.getServletContext());  
[java] view plain copy
 print?
  1. }  
  2.    
Spring执行实现Spring上下文的方法
[java] view plain copy
 print?
  1. public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {  
  2.   
  3.         if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) !=null) {  
  4.   
  5.             throw new IllegalStateException(  
  6.   
  7.                     "Cannot initialize context because there is already a root application context present - " +  
  8.   
  9.                     "check whether you have multiple ContextLoader* definitions in your web.xml!");  
  10.   
  11.         }  
  12.   
  13.    
  14.   
  15.         Log logger = LogFactory.getLog(ContextLoader.class);  
  16.   
  17.         servletContext.log("Initializing Spring root WebApplicationContext");  
  18.   
  19.         if (logger.isInfoEnabled()) {  
  20.   
  21.             logger.info("Root WebApplicationContext: initialization started");  
  22.   
  23.         }  
  24.   
  25.         long startTime = System.currentTimeMillis();  
  26.   
  27.    
  28.   
  29.         try {  
  30.   
  31.             // Store context in local instance variable, to guarantee that  
  32.   
  33.             // it is available on ServletContext shutdown.  
  34.   
  35.             if (this.context ==null) {  
  36.   
  37.                 this.context = createWebApplicationContext(servletContext);  
  38.   
  39.             }  
  40.   
  41.             if (this.context instanceof ConfigurableWebApplicationContext) {  
  42.   
  43.                 configureAndRefreshWebApplicationContext((ConfigurableWebApplicationContext)this.context, servletContext);  
  44.   
  45.             }  
  46.   
  47.             servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,this.context);  
  48.   
  49.    
  50.   
  51.             ClassLoader ccl = Thread.currentThread().getContextClassLoader();  
  52.   
  53.             if (ccl == ContextLoader.class.getClassLoader()) {  
  54.   
  55.                 currentContext = this.context;  
  56.   
  57.             }  
  58.   
  59.             else if (ccl != null) {  
  60.   
  61.                 currentContextPerThread.put(ccl, this.context);  
  62.   
  63.             }  
  64.   
  65.    
  66.   
  67.             if (logger.isDebugEnabled()) {  
  68.   
  69.                 logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +  
  70.   
  71.                         WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");  
  72.   
  73.             }  
  74.   
  75.             if (logger.isInfoEnabled()) {  
  76.   
  77.                 long elapsedTime = System.currentTimeMillis() - startTime;  
  78.   
  79.                 logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");  
  80.   
  81.             }  
  82.   
  83.    
  84.   
  85.             return this.context;  
  86.   
  87.         }  
  88.   
  89.         catch (RuntimeException ex) {  
  90.   
  91.             logger.error("Context initialization failed", ex);  
  92.   
  93.             servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);  
  94.   
  95.             throw ex;  
  96.   
  97.         }  
  98.   
  99.         catch (Error err) {  
  100.   
  101.             logger.error("Context initialization failed", err);  
  102.   
  103.             servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);  
  104.   
  105.             throw err;  
  106.   
  107.         }  
  108.   
  109.     }  
0 0
原创粉丝点击