Spring之ContextLoaderListener的作用 分享

Spring org.springframework.web.context.ContextLoaderListener

public class ContextLoaderListener

extends Object implements ServletContextListener

作用:在啓動Web容器時,自動裝配SpringapplicationContext.xml的配置信息。

因爲它實現了ServletContextListener這個接口,在web.xml配置這個監聽器,啓動容器時,就會默認執行它實現的方法。在ContextLoaderListener中關聯了ContextLoader這個類,所以整個加載配置過程由ContextLoader來完成。

看看ContextLoader的API說明

第一段說明ContextLoader可以由ContextLoaderListenerContextLoaderServlet生成。如果查看ContextLoaderServletAPI,可以看到它也關聯了ContextLoader這個類而且它繼承了HttpServlet

第二段,ContextLoader創建的是XmlWebApplicationContext這樣一個類,它實現的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->

BeanFactory這樣一來spring中的所有bean都由這個類來創建

第三段,講如何部署applicationContextxml文件,如果在web.xml中不寫任何參數配置信息,默認的路徑是"/WEB-INF/applicationContext.xml,在WEB-INF目錄下創建的xml文件的名稱必須是applicationContext.xml。如果是要自定義文件名可以在web.xml里加入contextConfigLocation這個context參數:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext-*.xml
</param-value>
</context-param>

<param-value> </param-value>裏指定相應的xml文件名,如果有多個xml文件,可以寫在一起並一“,”號分隔。上面的applicationContext-*.xml採用通配符,比如這那個目錄下有applicationContext-ibatis-base.xmlapplicationContext-action.xmlapplicationContext-ibatis-dao.xml等文件,都會一同被載入。

-----------------------------------------------------------------------------------------

例子,在web.xml文件中加入下面代碼

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

如果applicationContext.xml文件沒有在/WEB-INF/下,或文件名不一致,或存在多個Spring配置文件,在web.xml文件中根據下面代碼修改

  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath*:applicationContext-*.xml,/WEB-INF/applicationContext.xml,/WEB-INF/classes/applicationContext-*.xml
</param-value>
  </context-param>

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////1 Spring之ContextLoaderListener的作用 
http://lei2006.blog.sohu.com/116206469.html 

使用spring除了添加必要的jar包,另外在web.xml一定要加上啓動spring的監聽器,這樣配置在xml文件中的bean纔會初始化 

如你在web.xml這樣作了配置:(web.xml 2.4) 
Java代碼  收藏代碼
  1. <listener>  
  2.         <listener-class>  
  3.             org.springframework.web.context.ContextLoaderListener  
  4.         </listener-class>  
  5.     </listener>     

它會默認查找位於:WEB-INF/下的是否有一個文件名稱爲:applicationContext.xml 
如果沒有就會報錯: 
嚴重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener 
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] 
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:509)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:427) 
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:294) 
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:215) 
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) 
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4521) 
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5004) 
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:4999) 
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) 
at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 
at java.lang.Thread.run(Thread.java:619) 
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] 
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:117)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 20 more
 
但在很多項目中可能會把配置文件集中管理,常見位置在:classpath下面,這樣的話,你要在web.xml中在配置另外一個節點名稱: 
Java代碼  收藏代碼
  1. <context-param>  
  2.         <param-name>  
  3.             contextConfigLocation  
  4.         </param-name>  
  5.         <param-value>  
  6.             classpath*:applicationContext.xml  
  7.         </param-value>  
  8.     </context-param>  

常見可能是這樣:/WEB-INF/classes/applicationContext-*.xml 
總之它是使用spring必須要配置的元素,一定不要少了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章