【Spring启动】idea spring启动报错: classpath resource [***.xml] cannot be opened because it does not exist

九月 11, 2019 5:38:20 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6433a2: startup date [Wed Sep 11 17:38:20 CST 2019]; root of context hierarchy
九月 11, 2019 5:38:20 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [.idea/applicationContext.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [.idea/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist(AbstractXmlApplicationContext.java:93)
 

出现以上错误信息,最直接/根本原因酒是没有找到applicationContext.xml配置文件;

出现此问题的根本原因是实例ApplicationContext时使用了以下方法来获取配置文件:

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

但是,ClassPathXmlApplicationContext( ) 方法是在其所在的目录中寻找 .xml 配置文件
注意: 这里指的是编译后的 .class 文件所在的目录,不是 .java 文件

通常习惯 applicationContext.xml 文件在 src 目录下,但是IDEA 默认的项目结构导致的(将 .java 文件和 .class 文件分开存放,.java文件存于 src 中,.class 文件存于 target 中),且idea默认是从target中读取配置文件的,因此,ClassPathXmlApplicationContext( ) 方法无法找到 applicationContext.xml。

那如何解决呢?此时只需要将idea默认配置修改成src目录即可。

注意:不能直接把 applicationContext.xml 移至 target 目录下,因为 .xml 配置文件运行时也需要在 .java 文件中获取属性信息

点击 File -> Project Structure(或快捷键 Ctrl+Alt+Shift+S)

修改后,点击apply->ok,重启idea,重新运行即可。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误是由于无法找到指定路径下的配置文件导致的。根据引用中的描述,当运行打包完成的jar文件时,会报错java.io.FileNotFoundException: class path resource [mybatis/mybatis-config.xml cannot be opened because it does not exist。问题出在配置文件路径的大小写问题上。在application.yaml文件中配置的路径是mybatis/mybatis-config.xml,而实际上配置文件的路径是mybatis/mybatis-confiy.xml。 解决这个问题的方法是将路径中的小写的m改成大写的M,即将mybatis-config.xml改成mybatis-confiy.xml。然后使用maven clean并package项目,问题就会解决。这个错误比较隐蔽,因为在编译和调试时是可以通过的,只有在运行打包后的jar文件时才会出现问题。 需要注意的是,在编写配置文件路径时,不仅要确保单词拼写正确,还要注意大小写。在application.yaml中写配置文件路径时,大小写不能写错。这是因为编译和调试过程中不会发现大小写错误,只有在运行打包后的jar文件时才会出现问题。 总结起来,解决这个错误的关键是检查配置文件路径的大小写拼写是否正确,并确保在打包后的jar文件中能正确找到配置文件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [运行jar包报错:[mybatis-config.xml] cannot be opened because it does not exist](https://blog.csdn.net/weixin_44239774/article/details/127832230)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【springboot】class path resource [mybatis/mybatis-config.xml] cannot be opened because it does not ...](https://blog.csdn.net/Mrerlou/article/details/120723471)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值