相关推荐recommended
spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......
作者:mmseoamin日期:2023-12-14

下面这几行代码大家应该都很熟悉,就是 Spring 的 "Hello World"。

spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第1张

 那么我们在运行这个 main方法可能会遇到【报错】:Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring.xml] cannot be opened because it does not exist......就像这样:

spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第2张

大概意思就是没有找到 spring.xml 这个配置文件,仔细看 spring.xml 是在 resource 目录下,貌似是没啥问题

spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第3张

再展开看下 target 目录发现了问题所在,这是因为在 Idea 中,编译器只会把 src/main/java 也就是源代码目录下的 .java 文件编译成 .class 文件然后放到 target 目录中,其他的默认不会。

spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第4张

 这里我们只需要将 spring.xml 文件复制到 target/classes/ 下即可解决

spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第5张

然后再运行试一下,OK啦。

spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第6张