tags:

views:

903

answers:

3

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

my web.xml content is as follows,

<?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

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..

+1  A: 

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).

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.

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?

matt b
-1 The `ContextLoaderListener` uses (or should be using) the `contextConfigLocation` that's been specified as a `<context-param>`, in this case `/WEB-INF/Context.xml`. The two-context configuration is deliberate here.
skaffman
The `contextConfigLocation` *is* specified as a `context-param`, it's right at the top of the file. It's also specified as an `init-param` to the servlet.
skaffman
skaffman is correct - I missed that when I originally read it - however I'm wondering now if unknown has the correct Spring version
matt b
Sorry my mistake, I missed that. For some reason I thought context params were specified after listeners, so I was just ignoring the top of the file.
Jason Gritman
I am using spring version 2.5.6.
Antony
A: 

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

<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.

Jason Gritman
Context.xml has only one bean defined 'ArrayList' that too is for test purpose.
Antony
A: 

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

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

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.

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...

Antony