Spring启动报错--class path resource [Beans.xml] cannot be opened because it does not exist

推荐阅读

智能化校园:深入探讨云端管理系统设计与实现(一)
智能化校园:深入探讨云端管理系统设计与实现(二)


文章目录

  • 推荐阅读
      • 报错
      • 报错原因:
      • 解决方案:


报错

以下是idea的报错提示。

 class path resource [Beans.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:333)

报错原因:

出现以上错误信息,最直接/根本原因就是没有找到applicationContext.xml配置文件;
出现此问题的根本原因是实例ApplicationContext时使用了以下方法来获取配置文件:

ApplicationContext ctx = new ClassPathXmlApplicationContext("Beans.xml");

ClassPathXmlApplicationContext( ) 方法是在其所在的目录中寻找 .xml 配置文件,
而该目录实际上是指的是编译后的 .class 文件所在的目录,不是 .java 文件。
由于idea默认的目录结构它将 .java 文件和 .class 文件分开存放,.java文件存于 src 中,.class 文件存于 target 中。

所以,原本的Beans.xml并不在编译后的.class文件目录下,ClassPathXmlApplicationContext( ) 方法无法找到 Beans.xml

解决方案:

  • 可以先调整一下.class文件的输出目录,将其输出目录和Beans.xml所在目录保持一致即可。

Spring启动报错--class path resource [Beans.xml] cannot be opened because it does not exist_第1张图片
目录结构:
Spring启动报错--class path resource [Beans.xml] cannot be opened because it does not exist_第2张图片
image.png

  • 也可以将Beans.xml文件复制到target/classes/路径下,也可成功解决。

Spring启动报错--class path resource [Beans.xml] cannot be opened because it does not exist_第3张图片

你可能感兴趣的:(bug,程序报错,Spring,spring,xml,java)