0

I have a program I'm running in a Spring project which always fails because of a java.io.FileNotFoundException, when it comes to locating the DispatcherServlet.

The DispatcherServlet lives in the \WEB-INF folder and is accessible to the rest of the project without incident.

So at the moment I'm forced to hardcode the path to the DispatcherServlet as follows:

File config = new File("C:\\project\\build\\web\\WEB-INF\\project-servlet.xml");
    boolean exists = Misc.checkFileExists(config.getAbsolutePath());
    if (exists) {
        System.out.println("File: " + config.getAbsolutePath() + " found.");
    }
    ConfigurableApplicationContext context = new FileSystemXmlApplicationContext(config.getAbsolutePath());`

Which is not the best way at all.

But if I try to place the DispatcherServlet in a folder under \WEB-INF, e.g. \WEB-INF\resources to satisfy the CLASSPATH, the file is still not found. Because of this I can't use ClassPathXmlApplicationContext.

I have resolved this by setting my web.xml file as follows:

<servlet>
    <servlet-name>project</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/project-servlet.xml</param-value>
    </init-param>      
</servlet>     

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> 
<context-param>        
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/project-servlet.xml</param-value>
</context-param>  

The application works as does the test program with:

ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("project-servlet.xml");

I should add that I have a single project-servlet.xml file which configures everything.

Mr Gwent
  • 57
  • 1
  • 8
  • What is the complete exception stack trace, and what is the code causing this stack trace? – JB Nizet Jul 06 '14 at 12:59
  • The WEB-INF folder is not on the classpath. WEB-INF/classes and WEB-INF/lib folders are included in the classpath by default. Also try using the WebApplicationContext using a ContextLoaderListener within your web.xml to configure you Spring context. – dinukadev Jul 06 '14 at 13:30

1 Answers1

0

The code makes no sense. ClassPathXmlApplicationContext, as its name indicates, is used to load an XML file from a classpath resource. But you're passing it the path of a file. Read the javadoc:

Standalone XML application context, taking the context definition files from the class path, interpreting plain paths as class path resource names that include the package path (e.g. "mypackage/myresource.txt")

What constitutes the classpath of a web application is the directory WEB-INF/classes, and all the jar files inside WEB-INF/lib. So, your XML file is not in the CLASSPATH, and can thus not be loaded using a ClassPathXmlApplicationContext.

If you want to load a context file from WEB-INF, use an XmlWebApplicationContext.

You should not have to do that manually anyway, since a Spring web application is typically configured using the web.xml file, or using JavaConfig, as explained in the documentation.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255