Java spring 加载时 ContextLoaderListener 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1721381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:52:56  来源:igfitidea点击:

ContextLoaderListener problem while spring is loading up

javaspring

提问by Antony

When my spring is loading up, it says applicationContext.xmlis not found in the classpath by throwing FileNotFoundExceptionas can be seen below. Please help me resolve this problem.

当我的 spring 加载时,它说applicationContext.xml在类路径中找不到,FileNotFoundException如下所示。请帮我解决这个问题。

my web.xmlcontent is as follows,

我的web.xml内容如下,

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/Context.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>flex</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/web-spring-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <servlet-mapping>
        <servlet-name>flex</servlet-name>
        <url-pattern>/messagebroker/*</url-pattern>
    </servlet-mapping>
</web-app>

The exception being thrown is as follows,

抛出的异常如下,

SEVERE: 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 class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:135) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:297) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:280) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:131) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:147) at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:123) at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:91) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:100) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:298) at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:156) at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:246) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:184) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525) at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:926) at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:889) at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492) at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1149) at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053) at org.apache.catalina.core.StandardHost.start(StandardHost.java:719) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) at org.apache.catalina.core.StandardService.start(StandardService.java:516) at org.apache.catalina.core.StandardServer.start(StandardServer.java:710) at org.apache.catalina.startup.Catalina.start(Catalina.java:578) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413) Nov 12, 2009 4:02:09 PM org.apache.catalina.core.ApplicationContext log

严重:异常发送上下文初始化事件到类 org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanDefinitionStoreException:IOException 从类路径资源 [applicationContext.xml] 解析 XML 文档;嵌套异常是 java.io.FileNotFoundException: 类路径资源 [applicationContext.xml] 无法打开,因为它不存在 引起:java.io.FileNotFoundException: 类路径资源 [applicationContext.xml] 无法打开,因为它不存在在 org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:135) 在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:297) 在 org.springframework.beans.factory.xml .

When I don't want any files to be named applicationContext.xml, why it has to look for it? as it has been clearly stated in web.xml..

当我不想命名任何文件时applicationContext.xml,为什么它必须寻找它?正如它在web.xml..中明确说明的那样

采纳答案by Antony

Sorry guys, it was due to a simple problem in tomcat.

抱歉各位,这是由于tomcat中的一个简单问题。

Long back I was working on a project which was not deleted from tomcat webapp folder. That was spring project too.

很久以前,我正在处理一个未从 tomcat webapp 文件夹中删除的项目。那也是春季项目。

I figured it out by commenting out all in web.xml in the current project I am working on. When I did so, it was still trying to load spring container. That's when I opened tomcat manager web application and squinted for the application that wasn't bootstrapped.

我通过在我正在处理的当前项目中注释掉 web.xml 中的所有内容来解决这个问题。当我这样做时,它仍在尝试加载弹簧容器。那是我打开 tomcat 管理器 Web 应用程序并眯着眼睛寻找未引导的应用程序的时候。

I found an application that was not bootstrapped in tomcat manager web application, then I deleted that application, now my application started working like charm...

我发现了一个没有在 tomcat manager web 应用程序中引导的应用程序,然后我删除了那个应用程序,现在我的应用程序开始像魅力一样工作......

回答by matt b

You've actually configured two different Spring bootstrap loaders - ContextConfigListener will attempt to load a context file (whose name defaults to applicationContext.xml), and then DispatcherServlet will be started second and attempt to load your /WEB-INF/web-spring-context.xml(DispatcherServlet will load this context as a child of the first).

您实际上已经配置了两个不同的 Spring bootstrap 加载器 - ContextConfigListener 将尝试加载一个上下文文件(其名称默认为applicationContext.xml),然后 DispatcherServlet 将第二次启动并尝试加载您的/WEB-INF/web-spring-context.xml(DispatcherServlet 将加载此上下文作为第一个的子级) )。

If you don't need two contexts to load (or to have a parent and child context), you can remove the ContextConfigListener and only use DispatcherServlet.

如果您不需要加载两个上下文(或具有父子上下文),则可以删除 ContextConfigListener 并仅使用 DispatcherServlet。

update: What version of Spring are you using? The line numbers in your stacktrace do not match up with the source of org.springframework.web.context.ContextLoader that I have for Spring 2.5.6. Are you sure you are using the same version of spring-core and spring-webmvc?

更新:您使用的是哪个版本的 Spring?堆栈跟踪中的行号与 Spring 2.5.6 的 org.springframework.web.context.ContextLoader 源不匹配。您确定您使用的是相同版本的 spring-core 和 spring-webmvc 吗?

回答by Jason Gritman

Is your Context.xml trying to do an import of applicationContext.xml from the classpath? The code would look like this:

您的 Context.xml 是否试图从类路径导入 applicationContext.xml?代码如下所示:

<import resource="classpath:applicationContext.xml"/>

Based on the stacktrace, it's trying to load that file from the classpath (see the 2nd to last entry). I just tried on my machine and if it can't find the root context file the 2nd line of the trace is ServletContextResource.getInputStream, so it's reading Context.xml.

基于堆栈跟踪,它试图从类路径加载该文件(请参阅倒数第二个条目)。我刚刚在我的机器上尝试过,如果它找不到根上下文文件,则跟踪的第二行是ServletContextResource.getInputStream,因此它正在读取 Context.xml。