用 Maven 创建 Spring 项目找不到 applicationContext.xml 文件的解决方法

错误信息:

Exception in thread "main" 
org.springframework.beans.factory.BeanDefinitionStoreException: 
IOException parsing XML document from class path resource [applicationContext.xml];
nested exception is java.io.FileNotFoundException: class path resource 
[applicationContext.xml] cannot be opened because it does not exist

image-20200301154321499

解决方法:

如果是用 Maven 创建 Spring 项目的话,是不可以把 applicationContext.xml 文件放在 scr/main/java (就是写代码的地方)目录下的,这个时候是找不到这个文件的。

因为 Maven 项目默认的系统根目录在 src/main/resources(从上面的报错信息第3行就可以发现这个问题了)。

所以我们要把 .xml 文件放在这个路径下,才能找到它
image-20200301145610207

补充:

若干不是用 Maven 来创建项目(直接用 Spring 框架创建),那么根目录为正常的src,这个时候只需要把xml文件放到该路径下即可。

image-20200301153808716

发布了8 篇原创文章 · 获赞 6 · 访问量 293

猜你喜欢

转载自blog.csdn.net/Hedon954/article/details/104593670