spring ContextLoaderListener的作用

瓜瓜東西發表於2014-04-20
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必須要配置的元素,一定不要少了 

相關文章