web.xml 配置 spring 之 contextConfigLocation 詳解(轉)


spring的應用初始化流程一直沒有搞明白,在網上找到下面內容:

我們在開發spring的項目當中基本上都會在web.xml通過:

  1. <context-param>  
  2.         <param-name>contextConfigLocation</param-name>  
  3.         <param-value>  
  4.         /WEB-INF/conf/application-*.xml  
  5.         </param-value>  
  6.     </context-param>  

來初始化各個spring的配置文件,但是我們只是知道這段代碼的功能, 並不是很清楚我們配置了這段代碼之后為什么就能去初始化配置文件。當然我們還會加上:

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

這 一個listener,我首先就會想contextConfigLocation這個一定能在ContextLoaderListener這個類當中找 到,打開了源碼,這個listener是實現了ServletContextListener這個接口的,這個接口只有兩個方法:

  1. public interface ServletContextListener  
  2.     extends EventListener  
  3. {  
  4.   
  5.     public abstract void contextInitialized(ServletContextEvent servletcontextevent);  
  6.   
  7.     public abstract void contextDestroyed(ServletContextEvent servletcontextevent);  
  8. }  

而且它是繼承了EventListener這個接口的,打開這個接口的代碼讓我大吃一驚,里面沒有方法啥都沒有:

  1. package java.util;  
  2.   
  3.   
  4. public interface EventListener  
  5. {  
  6. }  

而且還是java.util包下的,並不是spring之中的東西。

這樣找了之后沒有找到,往回退到ContextLoaderListener這個類的方法上,contextInitialized方法是用來初始化上下文的:

  1. public void contextInitialized(ServletContextEvent event)  
  2.     {  
  3.         contextLoader = createContextLoader();  
  4.         contextLoader.initWebApplicationContext(event.getServletContext());  
  5.     }  

方法中有個createContextLoader方法:

  1. protected ContextLoader createContextLoader()  
  2.     {  
  3.         return new ContextLoader();  
  4.     }  

這個方法返回了一個ContextLoader實例,進入到ContextLoader類中,按ctrl+f來尋找contextConfigLocation,這時沒有出現電腦的咚的聲音,找到了它:

  1. protected WebApplicationContext createWebApplicationContext(ServletContext servletContext, ApplicationContext parent)  
  2.         throws BeansException  
  3.     {  
  4.         Class contextClass = determineContextClass(servletContext);  
  5.         if(!(org.springframework.web.context.ConfigurableWebApplicationContext.class).isAssignableFrom(contextClass))  
  6.         {  
  7.             throw new ApplicationContextException("Custom context class [" + contextClass.getName() + "] is not of type [" + (org.springframework.web.context.ConfigurableWebApplicationContext.class).getName() + "]");  
  8.         } else  
  9.         {  
  10.             ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext)BeanUtils.instantiateClass(contextClass);  
  11.             wac.setParent(parent);  
  12.             wac.setServletContext(servletContext);  
  13.             wac.setConfigLocation(servletContext.getInitParameter("<span style="color:#ff0000;">contextConfigLocation</span>"));  
  14.             customizeContext(servletContext, wac);  
  15.             wac.refresh();  
  16.             return wac;  
  17.         }  
  18.     }  

通 過代碼,ConfigurableWebApplicationContext設置了從servletContext獲取到的參數的值,再進入 ConfigurableWebApplicationContext的代碼中,它只是一個接口,進入 StaticWebApplicationContext的setConfigLocation方法:

  1. public void setConfigLocation(String configLocation)  
  2.     {  
  3.         if(configLocation != null)  
  4.             throw new UnsupportedOperationException("StaticWebApplicationContext does not support config locations");  
  5.         else  
  6.             return;  
  7.     }  

這個方法中很奇怪,當參數不為空就拋出異常,查看spring的文檔:The StaticWebApplicationContext class does not support this method.說是此類不支持這個方法,這下子又卡住了。又要退回去,看這句:

  1. ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext)BeanUtils.instantiateClass(contextClass);  

spring使用BeanUtils來初始化contextClass這個類實例,contextClass是通過以下代碼得到的:

  1. protected Class determineContextClass(ServletContext servletContext)  
  2.         throws ApplicationContextException  
  3.     {  
  4.         String contextClassName = servletContext.getInitParameter("contextClass");  
  5.         if(contextClassName != null)  
  6.             try  
  7.             {  
  8.                 return ClassUtils.forName(contextClassName);  
  9.             }  
  10.             catch(ClassNotFoundException ex)  
  11.             {  
  12.                 throw new ApplicationContextException("Failed to load custom context class [" + contextClassName + "]", ex);  
  13.             }  
  14.         contextClassName = defaultStrategies.getProperty((org.springframework.web.context.WebApplicationContext.class).getName());  
  15.         try  
  16.         {  
  17.             return ClassUtils.forName(contextClassName, (org.springframework.web.context.ContextLoader.class).getClassLoader());  
  18.         }  
  19.         catch(ClassNotFoundException ex)  
  20.         {  
  21.             throw new ApplicationContextException("Failed to load default context class [" + contextClassName + "]", ex);  
  22.         }  
  23.     }  

這里使用了反射,再來看BeanUtils的instantiateClass方法:

  1. return instantiateClass(clazz.getDeclaredConstructor((Class[])null), null);  

通過反射得到contextClass的構造方法。下面是instantiateClass方法的重載,主要是下面兩句代碼:

  1. ReflectionUtils.makeAccessible(ctor);  
  2.             return ctor.newInstance(args);  

ctor是通過反射得到的contextClass的構造方法,args是構造方法當中的參數。這里為null,說明new了contextClass的無參構造方法。

這時又要退回到determineContextClass 這個方法中,我們主要看:

  1. contextClassName = defaultStrategies.getProperty((org.springframework.web.context.WebApplicationContext.class).getName());  

這句代碼,我們可以猜它是通過Properties的getProperty方法得到WebApplicationContext 的實例,這時我們又到了WebApplicationContext 這個接口當中,這個接口繼承了ApplicationContext這個接口,我們都知道我們進行spring開發都會通過

Application ctx=new FileSystemXmlApplicationContext("beans.xml");

或ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");

ServletContext servletContext = request.getSession().getServletContext();

ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);

這三種方法獲得一個ApplicationContext,然后就可以對配置文件當中的bean進行操作了。所以這里我們基本上已經搞清楚初始化spring配置文件的流程了。

總結:通過查看這幾個類的源代碼,java的反射使用范圍之廣再次體現出來。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM