【问题标题】:Eclipse not able to find Spring configuration fileEclipse 找不到 Spring 配置文件
【发布时间】:2012-10-03 06:50:52
【问题描述】:

我正在使用 Spring 并使用 Eclipse 和 Spring。我用 eclipse 编写了一个非常简单的 Spring 应用程序来在 bean 中注入一个属性。但是,当我运行我的应用程序时,Spring 抛出异常,似乎 Spring 无法找到 Spring 配置文件。下面是堆栈跟踪--

INFO: Loading XML bean definitions from class path resource [Beans.xml]
Exception in thread "main" 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
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)

我尝试了以下方法——在 ClassPathXmlApplicationContext 方法中给出完整路径,例如——

ApplicationContext context = new ClassPathXmlApplicationContext("C:/Users/devshankhasharm/workspace/FinalPowerShell/src/src/main/Beans.xml");

我还更新了 Windows 中的 ClassPath 变量,以添加我的 spring 配置文件的路径。但没有任何效果。任何想法将不胜感激。

【问题讨论】:

  • 正如错误所说,您需要在类路径中有 beans.xml,而您没有,您不能使用 ClassPathXmlApplicationContext 的完整路径。请发布您的项目结构和 beans.xml 的位置,如果您正在运行 tomcat,则 beans.xml 是否存在于 web-inf/classes
  • 正如我上面提到的,我已经在 windows 中添加了 classpath 变量来包含 Spring 配置 xml 文件。在 Eclipse 中是否需要做任何事情来更新类路径以获取我的配置文件。
  • 是的,右键eclipse项目,选择构建路径->配置构建路径,检查Source是否有beans.xml所在的文件夹。据我所知,windows中的类路径环境变量不被认为是通过eclipse计算类路径

标签: java spring spring-mvc dependency-injection


【解决方案1】:

试试这个

ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:Beans.xml");

当然你的Beans.xml 必须在类路径中。

更新或者可能

ApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/Beans.xml");

【讨论】:

  • 我也有无法加载Bean.xml的问题。虽然我将文件添加到类路径中,但我只能通过“classpath*:Beans.xml”加载。如何通过“file:Beans.xml”加载?
  • @StellaMaris 如果classpath* 工作正常,为什么需要通过file: 加载?
  • 因为我对为什么只有classpath* 起作用感兴趣。我还有另一个example,我尝试加载hibernate.cfg.xml,因此我不能使用classpath*
  • 是的,如果我尝试使用 new ClassPathXmlApplicationContext("file:spring/config/Bean.xml") 它会失败,尽管我将它添加到我的 Java Build Path > Sources。
  • @StellaMaris 你可以尝试使用file:的完整路径。
【解决方案2】:

Beans.xml 应该在类路径中。您不能为 ClassPathXmlApplicationContext 提供 xml 文件的完整物理路径。请检查Eclipse的构建路径中是否有Beans.xml。

【讨论】:

    【解决方案3】:

    当您为 Beans.xml 示例使用完整文件路径时,请使用

    ApplicationContext context = new GenericXmlApplicationContext("C:/Users/devshankhasharm/workspace/FinalPowerShell/src/src/main/Beans.xml");
    

    但不建议这样做。请改用 ClassPathXmlApplicationContext。

    然后将 Beans.xml 移动到类路径中。最简单的方法是,如果不使用 Maven,则将其移动到 java 源的根目录;如果使用 Maven,则将其移动到 src/main/resources

    【讨论】:

      【解决方案4】:

      如果不麻烦,请尝试使用Spring Tool Suite。它是一个基于 Eclipse 的 Spring 友好 IDE,因此您不必依赖 spring/maven 插件配置。您所要做的就是创建一个 Spring 项目而不是 Java 项目,其余所有设置都将为您处理。

      【讨论】:

        【解决方案5】:

        如果您使用的是 Spring Tool Suite (STS),则可能是在您创建 Maven 项目时,将 src/main/resources 目录配置为“排除:.”。换句话说,STS 将您的 src/main/resources 目录设置为默认将其所有内容排除在输出之外。

        如何解决:

        1. 项目属性 (Alt+Enter) -> Java 构建路径 -> 源代码
        2. 在 src/main/resources 上,您可能会看到“已排除:.
        3. 选择此项并点击编辑...
        4. 删除 . 排除模式
        5. 单击确定。瞧!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-12-20
          • 2015-01-30
          • 2015-12-23
          • 2014-04-09
          • 2013-01-07
          • 2015-09-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多