`

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)
<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必须要配置的元素,一定不要少了
分享到:
评论

相关推荐

    Spring的监听器ContextLoaderListener的作用

    Spring的监听器ContextLoaderListener的作用

    java解决org.springframework.web.context.ContextLoaderListener

    java解决org.springframework.web.context.ContextLoaderListener

    Web项目中使用Spring, 使用 Spring 的器监听器 ContextLoaderListener.docx

    二、 使用 Spring 的器监听器 ContextLoaderListener o1. maven依赖pom.xml o2. 注册监听器 ContextLoaderListener o3. 指定 Spring 配置文件的位置 o4. 获取Spring容器对象 在 Web 项目中使用 Spring 框架,首先...

    spring-web-2.5.jar

    org.springframework.web.context.ContextLoaderListener.class org.springframework.web.context.ContextLoaderServlet.class org.springframework.web.context.ServletConfigAware.class org.springframework.web....

    Spring-5.1.5源码

    Spring framework 5.1.5源码 Main: spring-web/org.springframework.web.context.ContextLoaderListener

    Spring MVC 入门实例

    17 &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener 18 19 20 21 &lt;filter-name&gt;encodingFilter 22 &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter 23 24 ...

    springweb3.0MVC注解(附实例)

    org.springframework.web.context.ContextLoaderListener &lt;/listener&gt; &lt;!-- Spring MVC 的Servlet,它将加载WEB-INF/annomvc-servlet.xml 的 配置文件, 以启动Spring MVC模块--&gt; &lt;servlet-name&gt;annomvc ...

    spring和hibernate整合

    org.springframework.web.context.ContextLoaderListener &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt; &lt;/listener&gt;

    整合struts2和spring源代码(可以直接在tomcat中运行)

    org.springframework.web.context.ContextLoaderListener 4.配置spring的配置文件(可以查看WEB-INF里面的applicationContext.xml文件) 注意:如果是默认,一定要放在WEB-INF中并文件名为...

    基于java的企业级应用开发:Spring的核心容器.ppt

    spring/applicationContext.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; &lt;/listener&gt; ApplicationContext * * * *...

    spring源代码解析

    下面我们使用ContextLoaderListener作为载入器作一个详细的分析,这个Servlet的监听器是根上下文被载入的地方,也是整个 Spring web应用加载上下文的第一个地方;从加载过程我们可以看到,首先从Servlet事件中得到...

    spring_MVC源码

    09. &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; 10. &lt;/listener&gt; 11. 12. &lt;servlet&gt; 13. &lt;servlet-name&gt;spring&lt;/servlet-name&gt; 14. &lt;servlet-class&gt;org.spring...

    Spring源码学习七:web应用自动装配Spring配置文件1

    在web应用启动读取web.xml时,发现配置了ContextLoaderListener,而ContextLoaderListener实现了ServletCo

    struts2-spring-plugin-2.1.2.jar

    struts2与spring的整合。导入struts2-spring-... spring监听器对应的API类为:org.springframework.web.context.ContextLoaderListener。 struts2-spring-plugin包为我们将struts2的对象工厂设置为spring的IoC容器,

    spring-simple-web:使用 Spring Framework 的简单 Web (WAR) 项目

    Web 应用程序使用 Spring Web 侦听器初始化,例如web.xml org.springframework.web.context.ContextLoaderListener 。 Spring Web 侦听器使用web.xml的contextConfigLocation上下文参数进行初始化。 此设置的...

    springmybatis

    mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in ...

    Spring MVC 框架应用实例

    org.springframework.web.context.ContextLoaderListener &lt;filter-name&gt;encodingFilter org.springframework.web.filter.CharacterEncodingFilter &lt;param-name&gt;encoding ...

    iLink:拉丁云代码测试Spring MVC

    参考(2)Eclipse和IDEA配置Maven2.Spring版本:5.0.73.MVC:Spring MVC配置(1)jar包:spring-webmvc.jar(2)web.xml配置SpringMVC监听类:org.springframework.web.context.ContextLoaderListener(3)SpringMVC核心配置...

    简单spring MVC 配置

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener &lt;servlet-name&gt;test &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet &lt;load-on-startup&gt;1 &lt;servlet-name&gt;...

    SPRING API 2.0.CHM

    ContextLoaderListener ContextLoaderPlugIn ContextLoaderServlet ContextRefreshedEvent ContextSingletonBeanFactoryLocator ControlFlow ControlFlowFactory ControlFlowFactory.Jdk13ControlFlow ...

Global site tag (gtag.js) - Google Analytics