下面这几行代码大家应该都很熟悉,就是 Spring 的 "Hello World"。
![spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第1张 spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第1张](/upload/website_attach/202312/1_BMKEDHWZBAPT8HHV.jpeg)
那么我们在运行这个 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入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第2张](/upload/website_attach/202312/1_XDWZ3HKNXW66H746.jpeg)
大概意思就是没有找到 spring.xml 这个配置文件,仔细看 spring.xml 是在 resource 目录下,貌似是没啥问题
![spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第3张 spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第3张](/upload/website_attach/202312/1_6CUCZCA3V6DPQJG5.jpeg)
再展开看下 target 目录发现了问题所在,这是因为在 Idea 中,编译器只会把 src/main/java 也就是源代码目录下的 .java 文件编译成 .class 文件然后放到 target 目录中,其他的默认不会。
![spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第4张 spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第4张](/upload/website_attach/202312/1_VTSGRJK72CPKM4FM.jpeg)
这里我们只需要将 spring.xml 文件复制到 target/classes/ 下即可解决
![spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第5张 spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第5张](/upload/website_attach/202312/1_3ANZSE68BQCJFRUG.jpeg)
然后再运行试一下,OK啦。
![spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第6张 spring入门第一坑,解决报错class path resource [spring.xml] cannot be opened because it does not exist......,第6张](/upload/website_attach/202312/1_XK2Z7FPTRCUC2UWK.jpeg)
上一篇:Scala环境搭建及安装