spring单元測试时发现的问题:

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring/spring-datasource-mogon.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring/spring-datasource-mogon.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.test.db.mongo.test.MongoTest.setUp(MongoTest.java:32)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.io.FileNotFoundException: class path resource [spring/spring-datasource-mogon.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 26 morez

载入配置文件的代码例如以下:

ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-datasource-mogon.xml");

之前在写单元測试时也遇到过类似的问题,查询、核对代码和载入方式都没有问题,就是报错找不到文件。百思不得其解,一直以为是和环境相关。

今天在调试mongo时,最终找到问题所在了

出现故障的情况:仅仅要更新过、刷新过project,就会报如上错误;

解决的方法:

1. 新建一个内容同样,名字不同的配置文件;比如。将配置文件/spring-datasource-mogon.xml的名字改动为/spring-datasource-mogon-1.xm

2. 改动代码的载入配置文件为:ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-datasource-mogon-1.xml");

3. 调试。发现没有上述问题了。

问题总结:

1. 更新project后,单元測试载入的project也会刷新,刷新的时候配置文件从project环境中删掉了。

2. 改动名字。相当于加入了一个新的文件,这时project环境中会跟着同步。

3. 因此程序在载入新文件时能够找到。老的找不到。

4. 单元測试载入的配置文件的路径没有找到。假设找到了能够直接将老配置文件加入到当中

5. 遗留问题: 问什么刷新project时,会将配置文件所有清空;这个怎样避免

6. project的编辑存放路径和project执行环境路径的差别是什么,project执行环境路径是什么。

转载于:https://www.cnblogs.com/zfyouxi/p/5363013.html

nested exception is java.io.FileNotFoundException: class path resource [spring/spring-datasource-mog相关推荐

  1. nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope

    根据 提示 ,逐步解决bug,逐层推进:比如,有一个错是IOException异常,Springmvc.xml找不到,就需要去找下是否有配置文件,如果有,怎么回事?第二次再深入发现是target下没有 ...

  2. Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be ope

    1.错误描述 java.lang.IllegalStateException: Failed to load ApplicationContextat org.springframework.test ...

  3. java.io.FileNotFoundException: class path resource [com/wisely/highlight_spring4/ch2/aware/text.txt]

    maven+spring在加载配置文件时报错: java.io.FileNotFoundException: class path resource [com/wisely/highlight_spr ...

  4. 解决java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does no

    解决java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does no ...

  5. java.io.FileNotFoundException: class path resource引发的问题

    FileNotFoundExeception引发的问题 出现的报错方式 java.io.FileNotFoundException: class path resource [D:/workspace ...

  6. 问题备忘: 将工程打包成jar包运行,就报java.io.FileNotFoundException: class path resource错误

    最近在在使用@Value注入文件碰到如下问题: 工程在在IntelliJ IDEA开发环境里正常运行,但是一旦将工程打包成jar包运行,就报java.io.FileNotFoundException: ...

  7. java.io.FileNotFoundException: class path resource异常处理

    java.io.FileNotFoundException: class path resource异常处理找不到什么xml啊,什么@Auto....注解不能用啊,什么鬼啊.如果你已经百度了半天还是没 ...

  8. java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be opened

    一般来说路径问题. 我检查了web.xml的行,发现我设置的是固定的springmvc,但我实际命名为springMVC,当然报错. 其实报错了通过耐心翻译很容易看出问题,path resource ...

  9. maven异常解决 java.io.FileNotFoundException: class path resource [applicationContext_*.xml]

    经过查阅得知 出现这种FIleNotFoundException 原因无非两点: 1.路径配置错误 2.编译有问题,文件不存在 具体方法解决请参考https://blog.csdn.net/hanxu ...

  10. java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened becaus

    今天在学Spring的时候遇到一个问题, 配置文件applicationContext.xml找不到,很是郁闷啊.文件明明在这,却找不到这种问题大多数是因为文件路径的不对引起的. 首先我的我的配置文件 ...

最新文章

  1. 北斗三号系统第九、十颗组网卫星三大看点
  2. python手册中文版apk-Go、Python 语言解析安卓 APK 包
  3. TensorFlow 2.0.0-RC0版发布,专注于简单性与易用性
  4. 1.Java(初级)编程教程(油管 thenewboston)学习笔记get user input
  5. java扑克发牌程序_Java多线程实现扑克牌发牌程序实例
  6. 一套扁平化界面风格的flex 皮肤
  7. python-操作数据库的练习
  8. 对编辑框进行赋值和取值
  9. 电信运营商IT系统建设概况
  10. 远古战争java_原始人单机手游 分享问一款原始人族群生存
  11. 输入年份和月份输出该月有多少天python_输入一个年份和月份,打印出该月份有多少天(考虑闰年),用switch语句编程...
  12. 奥特曼小分队之四(Work Breakdown Structure)
  13. ElastSearch整合SpringBoot模仿京东商城实现关键字高亮显示
  14. caffe配置 一生不可自决
  15. Python3中的多态 ,鸭子类型
  16. 快速入门:Slurm资源管理与作业调度系统
  17. 如何用织梦仿制php网站首页,DEDE织梦网站首页(排名)仿制实战操作
  18. 四周型文字环绕怎么设置_word艺术字四周型环绕怎么设置
  19. 7-5 最佳情侣身高差c语言
  20. 金山云冲刺港股拟双重主要上市:年营收90亿 为雷军力挺项目

热门文章

  1. 利用卷积神经网络(CNN)提取图片特征
  2. Keras指定GPU训练模式,设置GPU的使用量
  3. python实现七种方法去除列表中的重复元素
  4. C3927 “->“: 非函数声明符后不允许尾随返回类型等错误
  5. 修改服务器后账套不存在,金蝶KIS专业版环境配置常见问题
  6. 区块链 Solidity中uint转string 数字转字符串
  7. C++ concurrent_queue::try_pop 方法
  8. 【Django 2021年最新版教程16】pycharm model模型修改之后如何同步更新到mysql数据库
  9. fisco bcos Caliper部署 错误总结
  10. kubernetes视频教程笔记 (25)-集群调度-调度过程说明