【问题标题】:Spring Mvc java.io.FileNotFoundException - ApplicationContext.xmlSpring Mvc java.io.FileNotFoundException - ApplicationContext.xml
【发布时间】:2012-09-25 00:55:38
【问题描述】:

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

【问题讨论】:

  • 你能发布你的 web.xml 吗?你的异常说 spring 正在寻找类路径而不是 WEB-INF 文件夹
  • web.xml 看起来不错。您的 WEB-INF 文件夹中是否也有 crimetrack-servlet.xml?
  • 是的,它在同一个目录中,我只是将它复制到更高的目录到一个文件夹调用 \resources 中,它位于 WEB-INF\resources 中并且它有效。为什么?applicationContext.xml 位于 WEB-INF 的根目录。认为我需要对此进行解释,是否可以将其留在 \resources 文件夹中并将其从 WEB-INF 的根目录中删除?我是否应该将 crimetrack-servlet.xml 文件也移动到 \resources 目录中

标签: java xml spring-mvc java-io applicationcontext


【解决方案1】:

就我而言,我需要做的就是移动 applicationContext.xml from

src\main\webapp\WEB-INF\

src\main\resources\.

【讨论】:

    【解决方案2】:

    我必须坚持使用 WEB-INF 文件中的 /resources 目录,或者您可以随意命名。 classpath 会查看 WEB-INF 目录,但它会扫描该目录中的文件夹。我将 applicationContext.xmlservlet.xml 文件移动到 WEB-INF/resources 目录中,所以可以将其从WEB-INF 的根目录,无需维护 applicationContext.xmlservlet.xml 文件的两个副本。

    【讨论】:

      【解决方案3】:

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

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

      它会起作用的!!!

      【讨论】:

        猜你喜欢
        • 2011-08-07
        • 1970-01-01
        • 1970-01-01
        • 2012-02-02
        • 2017-02-12
        • 2011-01-12
        • 1970-01-01
        • 2014-04-27
        • 2011-03-30
        相关资源
        最近更新 更多