Eclipse无法找到Spring配置文件
我在Spring中弄湿了我的手,并与Spring一起使用Eclipse。我已经用eclipse写了一个非常简单的Spring应用程序来在bean中注入一个属性。但是,当我运行我的应用程序时,Spring正在抛出异常,并且Spring似乎无法找到Spring配置文件。下面是堆栈跟踪 -Eclipse无法找到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");
我也更新了CLASSPATH变量在Windows中添加我的弹簧配置文件的路径。但没有任何工作。任何想法将不胜感激。
Beans.xml应该放在classpath中。您无法为ClassPathXmlApplicationContext提供完整的xml文件物理路径。请检查Beans.xml是否存在于Eclipse的构建路径中。
试试这个
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:Beans.xml");
当然你Beans.xml
必须在类路径中。
更新也许
ApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/Beans.xml");
我也有问题,我不能加载Bean.xml。 \t虽然我将文件添加到类路径中,但只能通过“classpath *:Beans.xml”加载。我怎样才能通过“文件:Beans.xml”加载? – StellaMaris
@StellaMaris如果'classpath *'正在工作,为什么需要通过'file:'加载? –
因为我对只为'classpath *'工作的原因感兴趣。并且我还有其他的[示例](我尝试加载其中的一个[示例](http://stackoverflow.com/questions/36039062/load-hibernate-cfg-xml-in-existing-java-project/36039272?noredirect=1#comment59805602_36039272) hibernate.cfg.xml和因此我不能使用'classpath *'。 – StellaMaris
当你使用一个完整的文件路径为您的beans.xml例如,使用
ApplicationContext context = new GenericXmlApplicationContext("C:/Users/devshankhasharm/workspace/FinalPowerShell/src/src/main/Beans.xml");
,但不建议这样做。改为使用ClassPathXmlApplicationContext。
然后将Beans.xml移动到类路径中。最简单的方法是,如果使用Maven,如果不使用Maven或src/main/resources,则将其移动到Java源代码的根目录下。
如果没什么麻烦的话,可以尝试使用Spring Tool Suite。它是基于Eclipse的Spring友好的IDE,因此您不必依赖于spring/maven插件配置。所有你需要做的就是去创建一个Spring项目而不是Java项目,并且休息所有的设置将会为你处理。
如果您在使用Spring工具套件(STS),它可能是,当你创建一个Maven项目中的src/main/resources目录与配置“除外:”的情况。换句话说,默认情况下,STS会将您的src/main/resources目录设置为从输出中排除其所有内容。
如何解决此问题:
- 项目属性(Alt + Enter键) - > Java构建路径 - >源
- 上的src/main /资源,你可能会看到 “除外:。”
- 选择该项目并点击编辑...
- 删除。排除模式
- 单击确定。瞧!
由于错误提示您需要在classpath中有beans.xml,而您没有,所以无法使用ClassPathXmlApplicationContext的完整路径。请发布你的项目结构和bean.xml的位置,如果你正在运行tomcat,那么beans.xml会出现在web-inf/classes中 – Subin
正如我上面提到的,我在windows中添加了classpath变量来包含Spring配置xml文件。有没有什么需要在Eclipse中完成更新类路径来获取我的配置文件。 – user496934
yes右键单击eclipse项目,选择build path-> configure build path并检查Source是否具有beans.xml所在的文件夹。我认为windows中的类路径环境变量不被认为是通过eclipse计算类路径 – Subin