Spring Mvc java.io.FileNotFoundException - ApplicationContext.xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12574877/
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-10-31 09:25:47  来源:igfitidea点击:

Spring Mvc java.io.FileNotFoundException - ApplicationContext.xml

javaxmlspring-mvcjava-ioapplicationcontext

提问by devdar

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

applicationContext.xml 位于 WEB-INF 文件夹中,为什么我收到此错误:

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

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

采纳答案by devdar

i had to stick to using the /resourcesdirectory in the WEB-INFfile or you can name it anything you want. The classpathlooks at the WEB-INFdirectory however it scans the folders within that directory. I moved the applicationContext.xmland the servlet.xmlfiles into the WEB-INF/resourcesdirectory, so yes it can be deleted from the root of the WEB-INFand there is no need to maintain two copies of an applicationContext.xmlor servlet.xmlfiles.

我不得不坚持使用WEB-INF文件中的/resources目录,或者您可以随意命名它。在类路径着眼于WEB-INF目录但它会扫描该目录中的文件夹。我将applicationContext.xmlservlet.xml文件移动到WEB-INF/resources目录中,所以是的,它可以从WEB-INF的根目录中删除,并且不需要维护applicationContext.xml 的两个副本或servlet.xml文件。

回答by Nikita Bosik

In my case all I need to do is to moveapplicationContext.xmlfrom

在我来说,我需要做的是移动applicationContext.xml

src\main\webapp\WEB-INF\

src\main\webapp\WEB-INF\

to

src\main\resources\.

src\main\resources\.

回答by chamzz.dot

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

您需要做的是将您的ApplicationContext.xml文件添加到您的源目录中。然后像这样加载没有任何路径的spring配置文件。

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

it will work !!!

它会工作!!!