Spring ContextLoaderListener源碼分析 .

http://blog.csdn.net/hz_blog/article/details/7688921

我們要自動裝配ApplicationContext配置信息時候,首先在web.xml配置ContextLoaderListener,下面是部分源代碼:

[java] view plaincopy
  1. public class ContextLoaderListener implements ServletContextListener {  
  2.   
  3.     private ContextLoader contextLoader;  
  4.   
  5.   
  6.     /** 
  7.      * Initialize the root web application context. 
  8.      */  
  9.     public void contextInitialized(ServletContextEvent event) {  
  10.         this.contextLoader = createContextLoader();  
  11.         this.contextLoader.initWebApplicationContext(event.getServletContext());//注意此處  
  12.     }  
  13.   
  14.     protected ContextLoader createContextLoader() {  
  15.         return new ContextLoader();  
  16.     }  
  17.   
  18.     public ContextLoader getContextLoader() {  
  19.         return this.contextLoader;  
  20.     }  
  21.   
  22.     public void contextDestroyed(ServletContextEvent event) {  
  23.         if (this.contextLoader != null) {  
  24.             this.contextLoader.closeWebApplicationContext(event.getServletContext());  
  25.         }  
  26.     }  
  27.   
  28. }  
我們主要了解下WebApplicationContext是怎麼初始化出來的,首先contextInitialized(ServletContextEvent event)方法的this.contextLoader.initWebApplicationContext(event.getServletContext());進去之後我們看到

[java] view plaincopy
  1. public WebApplicationContext initWebApplicationContext(ServletContext servletContext)  
  2.         throws IllegalStateException, BeansException {  
  3.   
  4.     if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {  
  5.         throw new IllegalStateException(  
  6.                 "Cannot initialize context because there is already a root application context present - " +  
  7.                 "check whether you have multiple ContextLoader* definitions in your web.xml!");  
  8.     }  
  9.   
  10.     servletContext.log("Initializing Spring root WebApplicationContext");  
  11.     if (logger.isInfoEnabled()) {  
  12.         logger.info("Root WebApplicationContext: initialization started");  
  13.     }  
  14.     long startTime = System.currentTimeMillis();  
  15.   
  16.     try {  
  17.         // Determine parent for root web application context, if any.  
  18.         ApplicationContext parent = loadParentContext(servletContext);  
  19.   
  20.         // Store context in local instance variable, to guarantee that  
  21.         // it is available on ServletContext shutdown.  
  22.         this.context = createWebApplicationContext(servletContext, parent);  
  23.         servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);//注意此處  
  24.         currentContextPerThread.put(Thread.currentThread().getContextClassLoader(), this.context);  
  25.   
  26.         if (logger.isDebugEnabled()) {  
  27.             logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +  
  28.                     WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");  
  29.         }  
  30.         if (logger.isInfoEnabled()) {  
  31.             long elapsedTime = System.currentTimeMillis() - startTime;  
  32.             logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");  
  33.         }  
  34.   
  35.         return this.context;  
  36.     }  
  37.     catch (RuntimeException ex) {  
  38.         logger.error("Context initialization failed", ex);  
  39.         servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);  
  40.         throw ex;  
  41.     }  
  42.     catch (Error err) {  
  43.         logger.error("Context initialization failed", err);  
  44.         servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);  
  45.         throw err;  
  46.     }  
  47. }  

注意注意此處部分,說明當我們要得到一個WebApplicationContext只需要在ServletContext中獲取屬性名爲WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE的常量值就可以得到一個ApplicationContext,最終可以獲取spring配置文件中任意一個Bean對象

eg:

[java] view plaincopy
  1. public String execute() throws Exception {  
  2.     // TODO Auto-generated method stub  
  3.     ApplicationContext conn=(ApplicationContext)ServletActionContext.getServletContext().getAttribute(WebApplicationContext.class.getName() + ".ROOT");  
  4.     System.out.println(conn.getBean("biz"));  
  5.     return SUCCESS;  
  6. }  

或者直接使用org.springframework.web.context.support.WebApplicationContextUtils這個抽象的工具類來獲得WebApplicationContext,該類的方法

[java] view plaincopy
  1. public static WebApplicationContext getWebApplicationContext(ServletContext sc) {  
  2.     return getWebApplicationContext(sc, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);  
  3. }  

還有更多方法就不多介紹了,自行查看這個類的源碼。
發佈了47 篇原創文章 · 獲贊 21 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章