gpt4 book ai didi

java.io.FileNotFoundException : class path resource 错误

转载 作者:数据小太阳 更新时间:2023-10-29 02:39:47 29 4
gpt4 key购买 nike

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.*;

public class Test {
public static void main(String args[]){

Resource res= new ClassPathResource("E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(res);

Employee s=(Employee)factory.getBean("e");

}
}

以上是我的程序,错误显示是:

Oct 13, 2015 8:42:28 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.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:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
at contructor.Test.main(Test.java:21)
Caused by: java.io.FileNotFoundException: class path resource [E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)

可能是什么问题?请帮忙,因为我是 springs 的新手

我遇到了同样的问题,我该如何解决?

最佳答案

您正在尝试获取对文件的引用作为类路径资源,但您提供的路径不是类路径中文件的路径。

不要对文件的完整路径进行硬编码,而是使用相对于项目根源的路径。如果 src 是根源目录,使用这个:

Resource res = new ClassPathResource("/contructor/applicationContext.xml");

关于java.io.FileNotFoundException : class path resource 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33106591/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com