FileNotFoundException..Classpath 资源在 spring 中找不到?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3433109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FileNotFoundException..Classpath resource not found in spring?
提问by javanoob
I have code like this in Main.java:
我在Main.java 中有这样的代码:
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Until recently it was working, but I don't know why it started failing with the below exception:
直到最近它还在工作,但我不知道为什么它开始失败并出现以下异常:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring-config.xml] cannot be opened because it does not exist
线程“main” org.springframework.beans.factory.BeanDefinitionStoreException 中的异常:IOException 从类路径资源 [spring-config.xml] 解析 XML 文档;嵌套异常是 java.io.FileNotFoundException: 类路径资源 [spring-config.xml] 无法打开,因为它不存在
the spring-config.xmlis in src/main/resourcesfolder.
所述弹簧-config.xml中是在SRC /主/资源文件夹中。
Actually I wanted to learn about the annotations: @Postconstruct and @Predestroy, so I changed the build path to Jdk 1.6 from Jdk 1.5.
其实我想了解注释:@Postconstruct 和@Predestroy,所以我将构建路径从 Jdk 1.5 更改为 Jdk 1.6。
Since then the problem started...
从那以后问题就开始了……
Any clue why it is not working?
任何线索为什么它不起作用?
NOTE: If any wants to see my project structure please follow this link http://code.google.com/p/javapracticeram/source/browse/trunk/SpringExample/
注意:如果有人想查看我的项目结构,请点击此链接 http://code.google.com/p/javapracticeram/source/browse/trunk/SpringExample/
回答by tolitius
Looking at your classpathyou exclude src/main/resourcesand src/test/resources:
查看您排除的类路径src/main/resources并src/test/resources:
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
Is there a reason for it? Try not to exclude a classpath to spring-config.xml:)
有什么原因吗?尽量不要排除类路径spring-config.xml:)
回答by earldouglas
Check the contents of SpringExample/target/classes. Is spring-config.xml there? If not, try manually removing the SpringExample/target/ directory, and force a rebuild with Project=>Clean... in Eclipse.
检查 SpringExample/target/classes 的内容。有 spring-config.xml 吗?如果没有,请尝试手动删除 SpringExample/target/ 目录,并在 Eclipse 中使用 Project=>Clean... 强制重建。
回答by Ryan Stewart
Two things worth pointing out:
有两点值得指出:
- The scope of your spring-context dependency shouldn't be "runtime", but "compile", which is the default, so you can just remove the scope line.
You should configure the compiler plugin to compile to at least java 1.5 to handle the annotations when building with Maven. (Can also affect IDE settings, though Eclipse doesn't tend to care.)
<build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build>
- 你的 spring-context 依赖的范围不应该是“运行时”,而是“编译”,这是默认的,所以你可以删除范围行。
您应该将编译器插件配置为至少编译为 java 1.5 以在使用 Maven 构建时处理注释。(也可以影响 IDE 设置,尽管 Eclipse 并不关心。)
<build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build>
After that, reconfiguring your project from Maven should fix it. I don't recall exactly how to do that in Eclipse, but you should find it if you right click the project node and poke around the menus.
之后,从 Maven 重新配置您的项目应该可以修复它。我不记得在 Eclipse 中是如何做到这一点的,但是如果您右键单击项目节点并浏览菜单,您应该会找到它。
回答by YoK
This is due to spring-config.xml is not in classpath.
这是因为 spring-config.xml 不在类路径中。
Add complete path of spring-config.xml to your classpath.
将 spring-config.xml 的完整路径添加到您的类路径中。
Also write command you execute to run your project. You can check classpath in command.
还要编写您执行的命令来运行您的项目。您可以在命令中检查类路径。
回答by kaykae
I was getting the same problem when running my project. On checking the files structure, I realised that Spring's xml file was inside the project's package and thus couldn't be found. I put it outside the package and it worked just fine.
我在运行我的项目时遇到了同样的问题。在检查文件结构时,我发现 Spring 的 xml 文件在项目的包内,因此无法找到。我把它放在包装外面,效果很好。
回答by Satya Tiwari
Best way to handle such error-"Use Annotation".
spring.xml-<context:component-scan base-package=com.SpringCollection.SpringCollection"/>
处理此类错误的最佳方法-“使用注释”。spring.xml-<context:component-scan base-package=com.SpringCollection.SpringCollection"/>
add annotation in that class for which you want to use Bean ID(i am using class "First")-
在要使用 Bean ID 的类中添加注释(我正在使用类“First”)-
@Component
public class First {
公共课第一个{
Changes In Main Class**-
主类的变化**-
ApplicationContext context = new AnnotationConfigApplicationContext(First.class);use this.
ApplicationContext context = new AnnotationConfigApplicationContext(First.class); 用这个。


