我正在尝试使用 Spring 并将 Eclipse 与 Spring 一起使用。我用 eclipse 编写了一个非常简单的 Spring 应用程序,用于在 bean 中注入(inject)一个属性。但是,当我运行我的应用程序时,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 配置文件的路径。但没有任何效果。任何想法将不胜感激。
最佳答案
试试这个
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:Beans.xml");
当然,您的 Beans.xml
必须在类路径中。
更新或者可能
ApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/Beans.xml");
关于java - Eclipse 找不到 Spring 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12859342/