Spring Mvc java.io.FileNotFoundException - ApplicationContext.xml
Asked Answered
S

3

2

The applicationContext.xml is in the WEB-INF folder, why am i getting this error :

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

Web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="crimeTrack" version="3.0">

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
         <param-name>log4jConfigLocation</param-name> 
         <param-value>/WEB-INF/classes/log4j.properties</param-value>
     </context-param>

     <listener>
          <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
     </listener>

     <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener> 

    <servlet>
        <servlet-name>crimetrack</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>crimetrack</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
    <welcome-file>
      index.jsp
    </welcome-file>
    </welcome-file-list>

    <jsp-config>
        <taglib>
            <taglib-uri>/spring</taglib-uri>
            <taglib-location>/WEB-INF/tld/spring-form.tld</taglib-location>
        </taglib>
    </jsp-config>

</web-app>
Symmetry answered 25/9, 2012 at 0:55 Comment(3)
can you post your web.xml? your exception says that spring is looking in the classpath not in the WEB-INF folderInversely
The web.xml looks fine. Do you have a crimetrack-servlet.xml in your WEB-INF folder too?Inversely
yes its in the same directory, i just copied it higher up the directory into a folder call \resources which is in WEB-INF\resources and it worked. Why?, the applicationContext.xml was at the root of the WEB-INF. Think i need an explanation for this will it be ok to leave it in the \resources folder and delete it from the root of the WEB-INF? Should i move the crimetrack-servlet.xml file as well into the \resources directorySymmetry
S
1

i had to stick to using the /resources directory in the WEB-INF file or you can name it anything you want. The classpath looks at the WEB-INF directory however it scans the folders within that directory. I moved the applicationContext.xml and the servlet.xml files into the WEB-INF/resources directory, so yes it can be deleted from the root of the WEB-INF and there is no need to maintain two copies of an applicationContext.xml or servlet.xml files.

Symmetry answered 25/9, 2012 at 4:11 Comment(0)
P
2

In my case all I need to do is to move applicationContext.xml from

src\main\webapp\WEB-INF\

to

src\main\resources\.

Pandich answered 20/1, 2015 at 0:17 Comment(0)
S
1

i had to stick to using the /resources directory in the WEB-INF file or you can name it anything you want. The classpath looks at the WEB-INF directory however it scans the folders within that directory. I moved the applicationContext.xml and the servlet.xml files into the WEB-INF/resources directory, so yes it can be deleted from the root of the WEB-INF and there is no need to maintain two copies of an applicationContext.xml or servlet.xml files.

Symmetry answered 25/9, 2012 at 4:11 Comment(0)
C
0

What You have to do is add your ApplicationContext.xml file in to your source directory. And then load spring configuration file without any path like this.

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");

it will work !!!

Credulous answered 29/8, 2019 at 0:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.