java - Eclipse 找不到 Spring 配置文件

标签 java spring spring-mvc dependency-injection

我正在尝试使用 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/

相关文章:

java - 无法使用 DateTimeFormat 注释让 Spring MVC 解析日期时间

java - Spring mongodb 异常 HTTP Status 500 - 过滤器执行抛出异常

javascript - 为什么我在服务器客户端状态下得到 406 Not Acceptable ,甚至我的 spring Controller 也没有调用?

java - Spring MVC - 无法渲染 View

java - 如何在没有数据库的情况下管理 java 中的序列?

java - 与activemq的优先级

java - 如何在 Netbeans 中自动重建链接项目?

java - 作为 Java 1.3 的数据库,什么是文本文件的良好替代品?

Java Socket 创建需要更多时间

java - 如何向方法的参数添加 JSR 303 验证