spring ContextLoaderListener的作用
1 Spring之ContextLoaderListener的作用
http://lei2006.blog.sohu.com/116206469.html
使用spring除了新增必要的jar包,另外在web.xml一定要加上啟動spring的監聽器,這樣配置在xml檔案中的bean才會初始化
如你在web.xml這樣作了配置:(web.xml 2.4)
它會預設查詢位於: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中在配置另外一個節點名稱:
常見可能是這樣:/WEB-INF/classes/applicationContext-*.xml
總之它是使用spring必須要配置的元素,一定不要少了
http://lei2006.blog.sohu.com/116206469.html
使用spring除了新增必要的jar包,另外在web.xml一定要加上啟動spring的監聽器,這樣配置在xml檔案中的bean才會初始化
如你在web.xml這樣作了配置:(web.xml 2.4)
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </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中在配置另外一個節點名稱:
- <context-param>
- <param-name>
- contextConfigLocation
- </param-name>
- <param-value>
- classpath*:applicationContext.xml
- </param-value>
- </context-param>
常見可能是這樣:/WEB-INF/classes/applicationContext-*.xml
總之它是使用spring必須要配置的元素,一定不要少了
相關文章
- ContextLoaderListener作用詳解Context
- spring的作用Spring
- Spring的作用域以及RequestContextListener作用SpringContext
- Spring框架的基本作用Spring框架
- spring @component的作用Spring
- 【轉】ContextLoaderListener 和 DispatcherServletContextServlet
- Spring 過濾器DelegatingFilterProxy No WebApplicationContext found: no ContextLoaderListener registered?Spring過濾器FilterWebAPPContext
- Spring中Bean的作用域SpringBean
- Spring: InitializingBean的作用SpringBean
- Spring在ssh中的作用Spring
- Spring Bean作用域SpringBean
- spring中各個類的作用Spring
- Spring 當中的Bean 作用域SpringBean
- [spring-core]作用域Spring
- Spring中Bean的作用域有哪些?SpringBean
- No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registeredWebAPPContextServlet
- spring @component 的作用詳細介紹Spring
- spring bean的作用域和生命週期SpringBean
- Spring裝配Bean(六)Bean的作用域SpringBean
- spring的懶載入,有什麼作用?Spring
- Spring在開發專案中起的作用Spring
- Spring學習(二)Bean 作用域SpringBean
- Spring AOP 對Spring MVC的Controller切面攔截不起作用SpringMVCController
- spring_bean的作用域,spring使用外部屬性檔案和spring_SpEL(三)SpringBean
- Spring-bean作用域scope詳解SpringBean
- Spring Security配置好了不起作用Spring
- 詳解Spring中Bean的作用域與生命週期SpringBean
- Spring MVC + Mybatis + Spring Aop宣告式事務管理沒有作用SpringMVCMyBatis
- web.xml中的contextConfigLocation在spring中的作用WebXMLContextSpring
- 【面試普通人VS高手】Spring 中Bean的作用域有哪些?面試SpringBean
- spring心得2--bean的生命週期@Spring監聽器的作用@Spring初始化容器案例分析@web專案使用SpringBeanWeb
- Spring學習歷程---request,session與globalSession作用域SpringSession
- js的作用域、作用域鏈JS
- js的作用域和作用域鏈JS
- js的作用域與作用域鏈JS
- Spring中建立帶原型作用域bean5種方法Spring原型Bean
- Java中的volatile的作用和synchronized作用Javasynchronized
- Spring 框架基礎(02):Bean的生命週期,作用域,裝配總結Spring框架Bean