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時,最終找到問題所在了

出現故障的情況:僅僅要更新過、刷新過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執行環境路徑是什么。




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM