springframework.core.io.Resource始终抛出java.io.FileNotFoundException
我是Spring的初学者,我正在学习资源springframework.core.io.Resource始终抛出java.io.FileNotFoundException,java,spring,Java,Spring,我是Spring的初学者,我正在学习资源 package com.smart.beanfactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class BeanLifeCycle { private static void LifeCycleInBeanFactory() { Resource
package com.smart.beanfactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class BeanLifeCycle {
private static void LifeCycleInBeanFactory() {
Resource res = new ClassPathResource("classpath:com/smart/beanfactory/beans.xml");
try {
System.out.println(res.getURL());
} catch (Exception e) {
System.out.println(e.fillInStackTrace());
}
}
public static void main(String[] args) {
LifeCycleInBeanFactory();
}
}
我编写上述简单代码只是为了使用类路径加载.xml文件
以下是我的项目的层次结构:
然后我在Resource res=new ClassPathResource(“file:/Users/haoxu/Documents/code/Java/anotherchapter4/src/main/resources/com.smart/beanfactory/beans.xml”)中尝试了绝对文件路径代码>
但这也行不通。
它只是告诉我“java.io.FileNotFoundException:类路径资源[Users/haoxu/Documents/Code/java/anotherchapter4/src/main/resources/com.smart/beanfactory/beans.xml]无法解析为URL,因为它不存在
"
-----------更新--------
我将代码修改为:
package com.smart.beanfactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.File;
public class BeanLifeCycle {
private static void LifeCycleInBeanFactory() {
Resource res = new ClassPathResource("/com/smart/beanfactory/beans.xml");
try {
System.out.println(res.getURL());
} catch (Exception e) {
System.out.println(e.fillInStackTrace());
}
}
public static void main(String[] args) {
LifeCycleInBeanFactory();
}
}
但我仍然得到“java.io.FileNotFoundException:类路径资源[com/smart/beanfactory/beans.xml]无法解析为URL,因为它不存在”前缀classpath:
对于ClassPathResource
不是必需的classpath:
对于ClassPathResource
不需要前缀
Resource res = new ClassPathResource("/resources/com/smart/beanfactory/beans.xml");
用你的代码试试下面这一行
Resource res = new ClassPathResource("/resources/com/smart/beanfactory/beans.xml");
我将文件“beans.xml”复制并粘贴到目录“target/classes/com/smart/beanfactory/”中。层次结构如下所示
我的代码如下:
package com.smart.beanfactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.File;
public class BeanLifeCycle {
private static void LifeCycleInBeanFactory() {
Resource res = new ClassPathResource("com/smart/beanfactory/beans.xml");
try {
System.out.println(res.getURL());
} catch (Exception e) {
System.out.println(e.fillInStackTrace());
}
}
public static void main(String[] args) {
LifeCycleInBeanFactory();
}
}
结果是“file:/Users/haoxu/Documents/Code/Java/anotherchapter4/target/classes/com/smart/beanfactory/beans.xml”
"
这实际上是文件的路径。但是它在目标目录中。我认为这真的很奇怪,我将文件“beans.xml”复制并粘贴到目录“target/classes/com/smart/beanfactory/”中。层次结构如下所示
我的代码如下:
package com.smart.beanfactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.File;
public class BeanLifeCycle {
private static void LifeCycleInBeanFactory() {
Resource res = new ClassPathResource("com/smart/beanfactory/beans.xml");
try {
System.out.println(res.getURL());
} catch (Exception e) {
System.out.println(e.fillInStackTrace());
}
}
public static void main(String[] args) {
LifeCycleInBeanFactory();
}
}
结果是“file:/Users/haoxu/Documents/Code/Java/anotherchapter4/target/classes/com/smart/beanfactory/beans.xml”
"
这实际上是文件的路径。但是它在目标目录中。我认为这真的很奇怪。请参阅上的说明根据您的屏幕截图,classpath中没有beans.xml文件。请参阅“target/…”目录下的内容。运行appYes时,beans.xml文件必须自动出现在那里。我的IDE似乎没有将.xml文件放入target/dir。我自己做,然后代码工作!目标目录是生成的结果,您不应该向其中添加文件。同意。请参阅上的说明根据您的屏幕截图,classpath中没有beans.xml文件。请参阅“target/…”目录下的内容。运行appYes时,beans.xml文件必须自动出现在那里。我的IDE似乎没有将.xml文件放入target/dir。我自己做,然后代码工作!目标目录是生成的结果,您不应该向其中添加文件。您是否尝试过项目树中显示的“com.smart.beanfactory/beans.xml”
?我尝试过,但无法工作。java.io.FileNotFoundException:无法将类路径资源[com.smart.beanfactory/beans.xml]解析为URL,因为它可能不存在。它可能是目录,而不是com.smart.beqnfactory。您应该使用名为xml的普通文件夹或其他名称,然后在资源创建中使用“/xml/beans.xml”。是否尝试过“com.smart.beanfactory/beans.xml”
就像它在项目树中显示的那样?我尝试了它,但它无法工作。java.io.FileNotFoundException:class path resource[com.smart.beanfactory/beans.xml]无法解析为URL,因为它可能不存在。它可能是目录,而不是com.smart.beqnfactory。您应该使用名为xml的普通文件夹或其他名称,然后在资源创建中使用“/xml/beans.xml”。仍然获取java.io.FileNotFoundException:类路径资源[resources/com/smart/beanfactory/beans.xml]无法解析为URL,因为它不存在。它应该工作时包含“/”like“/resources/com/smart/beanfactory/beans.xml”这个?仍然获取java.io.FileNotFoundException:类路径资源[resources/com/smart/beanfactory/beans.xml]无法解析为URL,因为它不存在。它应该工作时包含“/”like“/resources/com/smart/beanfactory/beans.xml“这是什么?