Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Eclipse无法找到Spring配置文件_Java_Spring_Spring Mvc_Dependency Injection - Fatal编程技术网

Java Eclipse无法找到Spring配置文件

Java Eclipse无法找到Spring配置文件,java,spring,spring-mvc,dependency-injection,Java,Spring,Spring Mvc,Dependency Injection,我在春天弄湿我的手,在春天的时候使用Eclipse。我已经用eclipse编写了一个非常简单的Spring应用程序,在bean中注入属性。然而,当我运行我的应用程序时,Spring抛出异常,似乎Spring无法找到Spring配置文件。下面是stacktrace- INFO: Loading XML bean definitions from class path resource [Beans.xml] Exception in thread "main" org.springframewor

我在春天弄湿我的手,在春天的时候使用Eclipse。我已经用eclipse编写了一个非常简单的Spring应用程序,在bean中注入属性。然而,当我运行我的应用程序时,Spring抛出异常,似乎Spring无法找到Spring配置文件。下面是stacktrace-

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应该在classpath中。无法为ClassPathXmlApplicationContext提供xml文件的完整物理路径。请检查eclipse的构建路径中是否有Beans.xml。

试试这个

ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:Beans.xml");
当然,Beans.xml必须在类路径中

更新或者


在为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;如果不太麻烦,则尝试使用。它是一个基于Eclipse的Spring友好IDE,因此您不必依赖Spring/maven插件配置。您所要做的就是创建一个Spring项目,而不是Java项目,其余的所有设置都将为您处理。

如果您使用的是Spring工具套件STS,那么在创建Maven项目时,可能会将src/main/resources目录配置为Excluded:。。换句话说,STS将src/main/resources目录设置为默认情况下从输出中排除其所有内容

如何修复它:

项目属性Alt+Enter->Java构建路径->源 在src/main/resources上,您可能会看到Excluded:。 选择此项目并单击编辑。。。 拆下。排除模式 单击“确定”。瞧!
由于错误表明您需要在classpath中包含bean.xml,而您没有,因此不能为ClassPathXmlApplicationContext使用完整路径。请发布您的项目结构和beans.xml的位置,如果您正在运行tomcat,那么beans.xml是否存在于web inf/classes中正如我前面提到的,我已经在windows中添加了classpath变量以包含Spring配置xml文件。在eclipse中是否需要执行任何操作来更新类路径以获取我的配置文件。是的,右键单击eclipse项目,选择build path->configure build path并检查Source是否有beans.xml所在的文件夹。据我所知,windows中的Class path环境变量不被认为是eclipse计算类路径的工具。我还有一个问题,就是无法加载Bean.xml。虽然我将该文件添加到类路径中,但我只能通过classpath*:Beans.xml加载。如何通过文件Beans.xml加载?@StellaMaris如果classpath*正常工作,为什么需要通过文件加载:?因为我感兴趣的是为什么只有classpath*正常工作。我还有一个尝试加载hibernate.cfg.xml的地方,因此我不能使用classpath*。是的,如果我尝试使用新的ClassPathXmlApplicationContextfile:spring/config/Bean.xml,它会失败,尽管我将它添加到了我的Java Build Path>Sources中。@StellaMaris您可以尝试对文件使用完整路径:。
ApplicationContext context = new GenericXmlApplicationContext("C:/Users/devshankhasharm/workspace/FinalPowerShell/src/src/main/Beans.xml");