简体   繁体   English

Eclipse无法找到Spring配置文件

[英]Eclipse not able to find Spring configuration file

I am wetting my hands in Spring and using Eclipse along with Spring. 我在Spring中弄湿手,并使用Eclipse和Spring。 I have written a very simple Spring application with eclipse to inject a property in a bean. 我用eclipse编写了一个非常简单的Spring应用程序来在bean中注入一个属性。 However, when I am running my application, Spring is throwing exception and it seems that the Spring is not able to find the Spring configuration file. 但是,当我运行我的应用程序时,Spring正在抛出异常,似乎Spring无法找到Spring配置文件。 Below is the 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)

I have tried the following -- Give the full path in the ClassPathXmlApplicationContext method like -- 我尝试了以下内容 - 在ClassPathXmlApplicationContext方法中给出完整路径,如 -

ApplicationContext context = new ClassPathXmlApplicationContext("C:/Users/devshankhasharm/workspace/FinalPowerShell/src/src/main/Beans.xml");

I have also updated the ClassPath variable in windows to add the path for my spring configuration file. 我还更新了Windows中的ClassPath变量,为我的spring配置文件添加了路径。 But nothing worked. 但没有任何效果。 Any idea would be highly appreciated. 任何想法都将受到高度赞赏。

Try this 尝试这个

ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:Beans.xml");

And of course your Beans.xml must be in classpath. 当然,您的Beans.xml必须在classpath中。

Update or maybe 更新或者可能

ApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/Beans.xml");

Beans.xml should be in classpath. Beans.xml应该在classpath中。 You cannot give full physical path of xml file for ClassPathXmlApplicationContext . 您无法为ClassPathXmlApplicationContext提供xml文件的完整物理路径。 Please check if Beans.xml is there in build path of eclipse. 请检查Eclipse的构建路径中是否存在Beans.xml。

As you are using a full filepath for your Beans.xml example, use 在为Beans.xml示例使用完整文件路径时,请使用

ApplicationContext context = new GenericXmlApplicationContext("C:/Users/devshankhasharm/workspace/FinalPowerShell/src/src/main/Beans.xml");

BUT it is not recommended to do this. 但不建议这样做。 Use the ClassPathXmlApplicationContext for this instead. 请改用ClassPathXmlApplicationContext。

Then move Beans.xml into the classpath. 然后将Beans.xml移动到类路径中。 The simplest way to do this is to move it to the root of your java source if not using Maven or src/main/resources if using Maven 最简单的方法是,如果不使用Maven或src / main / resources,如果使用Maven,则将其移动到java源的根目录

If it's not much of a bother then try using Spring Tool Suite . 如果它不是很麻烦,那么尝试使用Spring Tool Suite It's a Spring friendly IDE based on Eclipse, so that you don't have to depend on spring/maven plugin configurations. 它是一个基于Eclipse的Spring友好型IDE,因此您不必依赖spring / maven插件配置。 All you have to do is go and create a Spring Project instead of Java project and rest all the settings will be handled for you. 您所要做的就是去创建一个Spring项目而不是Java项目,然后将为您处理所有设置。

If you are using Spring Tool Suite (STS), it may be the case that when you create a Maven project the src/main/resources directory was configured with "Excluded: . ". 如果您在使用Spring工具套件(STS),它可能是,当你创建一个Maven项目中的src / main / resources目录与配置的情况下,“排除:”。 In other words, STS sets your src/main/resources directory to have all its contents excluded from output by default. 换句话说,STS将您的src / main / resources目录设置为默认情况下从输出中排除其所有内容。

How to fix it: 如何解决:

  1. Project properties (Alt+Enter) -> Java Build Path -> Source 项目属性(Alt + Enter) - > Java构建路径 - >源
  2. On src/main/resources, you may see "Excluded: . " 在src / main / resources上,您可能会看到“Excluded : .
  3. Select this item and click on Edit... 选择此项并单击编辑...
  4. Remove the . 删除 exclusion pattern 排除模式
  5. Click OK. 单击确定。 Voila! 瞧!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM