【发布时间】: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