【问题标题】:Spring can't load xml configuration fileSpring无法加载xml配置文件
【发布时间】:2015-12-12 03:59:53
【问题描述】:

这个错误让我大吃一惊。所有的 junit 测试都很好,但是当我的应用程序作为另一个应用程序的插件加载时,我遇到了错误。 beans.xml 100% 位于 jar 文件的根目录,但由于某种原因,加载我的插件的主应用程序找不到该文件。为什么?

在我的应用程序的启动方法中,我调用 new ClassPathXmlApplicationContext("beans.xml");抛出

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist

new FileSystemXmlApplicationContext(getClass().getClassLoader().getResource("beans.xml").getPath()); 抛出

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from URL [file:/C:/path/to/my.jar!/beans.xml]; nested exception is java.io.FileNotFoundException: C:\path\to\my.jar!\beans.xml (The system cannot find the path specified)


UPD:问题是应用程序使用了自定义类加载器,它不会在根目录、META-INF 等中加载资源文件,而只加载类

【问题讨论】:

  • “jar 文件的根目录”是什么意思,你能检查一下它所在的路径吗?
  • @Jama 我的意思是 beans.xml 必须位于 jar 文件中。

标签: java xml spring spring-mvc


【解决方案1】:

如果您收到FileNotFoundException,那是因为该文件不在您要查找的位置。 beans.xml 文件必须位于 src/main/resources

【讨论】:

    【解决方案2】:

    您得到的 FileNotFoundException 确实指出 beans.xml 文件不在您认为的位置的 jar 中。我想看看那个罐子里面。例如,您可以打开 jar with the 7-zip tool。我发现当您遇到与您报告的错误类似的错误时,这可以提供有用的洞察力。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      • 2012-12-07
      相关资源
      最近更新 更多