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

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时,终于找到问题所在了

出现问题的情况:只要更新过、刷新过工程,就会报如上错误;

解决办法:

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

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

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


问题总结:

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

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

3. 因此程序在加载新文件时可以找到,老的找不到。

4. 单元测试加载的配置文件的路径没有找到,如果找到了可以直接将老配置文件添加到其中

5. 遗留问题: 问什么刷新工程时,会将配置文件全部清空;这个如何避免

6. 工程的编辑存放路径和工程运行环境路径的区别是什么,工程运行环境路径是什么。



  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
### 回答1: 嵌套异常是java.io.filenotfoundexception: class path resource。 这个异常通常是由于找不到指定的文件或资源而引起的。可能是文件路径不正确,或者文件不存在。需要检查代码中指定的文件路径是否正确,并确保文件存在。 ### 回答2: nested exception is java.io.filenotfoundexception: class path resource 是一个异常信息,表示在使用Java的类路径资源时发生了文件未找到的错误。 这个异常通常在尝试读取或加载类路径上的文件时抛出,但无法找到指定的文件。可能的原因包括以下几点: 1. 文件路径错误:检查文件路径是否正确,确保文件存在于指定的位置。如果文件被移到了其他位置,需要相应地更新路径。 2. 缺失依赖库:如果文件依赖于其他库或资源文件,检查这些依赖项是否位于正确的位置并且可访问。 3. 文件权限问题:检查文件的读取权限,确保当前用户或应用程序具有访问该文件的权限。 4. 资源文件未打包:在使用Java打包工具(如JAR或WAR)时,确保文件被正确地打包并位于正确的位置。 解决这个问题的方法包括: 1. 检查文件路径和名称是否正确,确保文件存在。 2. 在代码中使用相对路径或绝对路径来引用文件。 3. 检查类路径是否正确配置,并确保文件位于类路径上。 4. 检查应用程序的依赖项,并确认所需的库是否可用或正确引入。 5. 检查文件的读取权限,并确保当前用户或应用程序具有访问权限。 总之,nested exception is java.io.filenotfoundexception: class path resource 表示在使用Java的类路径资源时发生了文件未找到的错误,需要检查文件路径和配置,确保文件存在并且可访问。同时,也要确认依赖项是否正确配置,并检查文件的读取权限。 ### 回答3: 嵌套异常是java.io.FileNotFoundException: class path resource。 嵌套异常是指在程序执行过程中抛出的异常,而该异常又是由于另一个异常引起的。在这种情况下,嵌套异常是由java.io.FileNotFoundException引起的。这个异常是在程序中尝试读取一个不存在的文件时抛出的。 在Java中,文件可以通过文件路径或者类路径来引用。如果使用类路径,那么文件必须位于项目的类路径下,否则就会抛出FileNotFoundException异常。在这个特定的情况下,类路径资源是指应用程序正在尝试读取的文件位于类路径上,但实际上该文件并不存在。 为了解决这个问题,我们可以采取以下几个步骤: 1. 确保文件确实存在于项目的类路径下。可以检查文件是否位于正确的位置,并且经过编译和打包。 2. 检查文件的命名是否正确。可能存在文件名大小写问题或者拼写错误。 3. 如果文件确实存在于类路径上,但是仍然无法读取,那么可能是由于权限问题。确保权限设置正确,并且应用程序在运行时具有读取该文件的权限。 通过以上步骤,您应该能够解决该异常并成功读取文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值