java IOException 从类路径资源解析 XML 文档 - 文件不存在 - Spring
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25263978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IOException parsing XML document from class path resource - file does not exist - Spring
提问by Julio Castedo
I'm aware that this question about the spring xml config file not been found in execution has been asked several times before, but none of the other answers seems to work for me.
我知道这个关于在执行中未找到的 spring xml 配置文件的问题之前已经被问过几次,但其他答案似乎都不适合我。
I'm trying to run some test in a Maven Project with Spring and JUnit and I get this error all the time:
我正在尝试使用 Spring 和 JUnit 在 Maven 项目中运行一些测试,但我一直收到此错误:
T E S T S Running es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest 12-ago-2014 13:37:33 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners INFO: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext] 12-ago-2014 13:37:33 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2f8bbc98: startup date [Tue Aug 12 13:37:33 CEST 2014]; root of context hierarchy 12-ago-2014 13:37:33 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [nosaTenda-spring-config.xml] Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.225 sec <<< FAILURE!
Results :
Tests in error: es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest: Could not initialize class es.udc.jcastedo.NosaTenda.test.model.util.DbUtil
Tests run: 2, Failures: 0, Errors: 2, Skipped: 0
测试运行 es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest 12-ago-2014 13:37:33 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners INFO:无法实例化 TestExecutionListener [org.springframework.test. context.web.ServletTestExecutionListener]。指定自定义侦听器类或使默认侦听器类(及其所需的依赖项)可用。违规类:[javax/servlet/ServletContext] 12-ago-2014 13:37:33 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO:刷新 org.springframework.context.support.ClassPathXmlApplicationContext@2f8bbc98:启动日期 [8 月周二12 13:37:33 CEST 2014];上下文层次结构的根 12-ago-2014 13:37:33 org.springframework.beans.factory.xml。
结果 :
错误测试:es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest:无法初始化类 es.udc.jcastedo.NosaTenda.test.model .util.DbUtil
测试运行:2,失败:0,错误:2,跳过:0
This is a link to the complete trace of the surefire report: https://dl.dropboxusercontent.com/u/2635926/es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest.txt
这是对surefire报告完整跟踪的链接:https: //dl.dropboxusercontent.com/u/2635926/es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest.txt
Where you can find this lines:
在哪里可以找到此行:
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [nosaTenda-spring-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [nosaTenda-spring-config.xml] cannot be opened because it does not exist
Caused by: java.io.FileNotFoundException: class path resource [nosaTenda-spring-config.xml] cannot be opened because it does not exist
es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest Time elapsed: 0.003 sec <<< ERROR! java.lang.NoClassDefFoundError: Could not initialize class es.udc.jcastedo.NosaTenda.test.model.util.DbUtil at es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest.cleanDb(ProductoServiceTest.java:52)
引起:org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [nosaTenda-spring-config.xml]; 嵌套异常是 java.io.FileNotFoundException: 类路径资源 [nosaTenda-spring-config.xml] 无法打开,因为它不存在
引起:java.io.FileNotFoundException: 类路径资源 [nosaTenda-spring-config.xml] 无法打开,因为它不存在
es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest 已用时间:0.003 秒 <<< 错误!java.lang.NoClassDefFoundError:无法在 es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest.cleanDb(ProductoServiceTest.java:52) 处初始化类 es.udc.jcastedo.NosaTenda.test.model.util.DbUtil
DbUtil is an auxiliary class called by all tests, where the context is initialized:
DbUtil 是所有测试调用的辅助类,其中的上下文被初始化:
public class DbUtil {
static {
ApplicationContext context = new ClassPathXmlApplicationContext("nosaTenda-spring-config.xml");
transactionManager = (PlatformTransactionManager) context
.getBean("transactionManager");
productoDao = (ProductoDao) context.getBean("productoDao");
tiendaDao = (TiendaDao) context.getBean("tiendaDao");
}
...
public static void populateDb() {
...
}
public static void cleanDb() throws Throwable {
...
}
}
Apparently there lays the problem, as it seems the xml config file is not found no matter what and it breaks at the ClassPathXmlApplicationContext method call.
显然存在问题,因为似乎无论如何都找不到 xml 配置文件,并且它在 ClassPathXmlApplicationContext 方法调用处中断。
Theoretically, the spring config file is located where it should be, in src/main/resources.
理论上,spring 配置文件位于它应该在的位置,在 src/main/resources 中。
I have tried all combinations, from the qualified name "/NosaTenda/src/main/resources/nosaTenda-spring-config.xml" to "nosaTenda-spring-config.xml", with the same result.
我尝试了所有组合,从限定名称“/NosaTenda/src/main/resources/nosaTenda-spring-config.xml”到“nosaTenda-spring-config.xml”,结果相同。
I don't know what's wrong with the class path, or if the problem is elsewhere.
我不知道类路径有什么问题,或者问题是否出在其他地方。
EDIT
编辑
Here is the pom of the project, I just remembered that I added some filtering in the build-resources section, maybe I did something wrong there and that's the problem.
这是项目的pom,我只记得我在build-resources部分添加了一些过滤,也许我在那里做错了什么,这就是问题所在。
采纳答案by Julio Castedo
Ok, I found the solution, it was somewhere else.
好的,我找到了解决方案,它在其他地方。
I was using a Run Configuration with the forkMode noneparameter, as I was running a single test class. forkModeapparently is deprecated and was causing the problem. I replaced it with forkCount 0and worked just fine, it found the spring xml config file, and even the test version overwriting some parameters, so perfect. Now I just have to debug a ton of other errors that the configuration has :)
我正在使用带有forkMode none参数的运行配置,因为我正在运行一个测试类。forkMode显然已被弃用并导致了问题。我用forkCount 0替换了,效果很好,找到了spring xml配置文件,甚至测试版也覆盖了一些参数,太完美了。现在我只需要调试配置中的大量其他错误:)
INFO: Loading XML bean definitions from class path resource [nosaTenda-spring-config.xml]
12-ago-2014 17:42:09 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [nosaTenda-spring-config-test.xml]
12-ago-2014 17:42:09 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
INFO: Overriding bean definition for bean 'dataSource': replacing [Generic bean: class [org.springframework.jndi.JndiObjectFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [nosaTenda-spring-config.xml]] with [Generic bean: class [org.springframework.jdbc.datasource.SingleConnectionDataSource]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [nosaTenda-spring-config-test.xml]]
Thanks.
谢谢。
回答by kallehj
Generally your classpath would be .../src/main/java/, and ClassPathXmlApplicationContext would read from .../src/main/resources. Then your test classes would be in .../src/test/java/ and ClassPathXmlApplicationContext would read from .../src/test/resources.
通常,您的类路径是 .../src/main/java/,而 ClassPathXmlApplicationContext 将从 .../src/main/resources 中读取。然后您的测试类将在 .../src/test/java/ 中,ClassPathXmlApplicationContext 将从 .../src/test/resources 中读取。
So, check where your test application context is, and put a copy of the xml there.
因此,请检查您的测试应用程序上下文的位置,并将 xml 的副本放在那里。