IOException解析来自类路径资源的XML文档——文件不存在——Spring

时间:2022-12-01 11:28:21

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!

运行。udc. jcastedo.nosatenda.test.model.productoservice。ProductoServiceTest 12 -前- 2014 13:37:33 org.springframework.test.context。TestContextManager retrievetestexecutionlistener信息:无法实例化TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]。指定自定义侦听器类或使默认侦听器类(及其所需的依赖项)可用。违规类:[javax/servlet/ letservcontext] 12-ago-2014 13:37:33 org.springframe .context.support。ClassPathXmlApplicationContext prepareRefresh INFO:刷新org.springframe .context.support。ClassPathXmlApplicationContext@2f8bbc98:启动日期[Tue 8月12日13:37:33 CEST 2014];上下文层次结构的根12-ago-2014 13:37:33 org.springframe .beans.factory.xml。XmlBeanDefinitionReader loadbeandefinition INFO:从类路径资源[nosat -spring-config]加载XML bean定义。测试运行:2,失败:0,错误:2,跳过:0,时间过了:0.225秒< <失败!< p>

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

测试错误:es.udc.jcastedo.NosaTenda.test.model.productoService。ProductoServiceTest es.udc.jcastedo.NosaTenda.test.model.productoService。ProductoServiceTest:无法初始化类。udc.jcastedo. nosat .test.model.util. dbutil

Tests run: 2, Failures: 0, Errors: 2, Skipped: 0

测试运行: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

引起的:org.springframework.beans.factory。BeanDefinitionStoreException: IOException解析来自类路径资源的XML文档[nosaTenda-spring-config.xml];嵌套的异常io。文件名:类路径资源[nosat -spring-config。不能打开xml,因为它不存在

Caused by: java.io.FileNotFoundException: class path resource [nosaTenda-spring-config.xml] cannot be opened because it does not exist

引起的:java。文件名:类路径资源[nosat -spring-config。不能打开xml,因为它不存在

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)

es.udc.jcastedo.NosaTenda.test.model.productoService。产品服务运行时间:0.003秒 <错误!. lang。noclassdeffounderror:无法初始化class . udc.jcastedo.nosatenda.test.model.util。dbutil es.udc.jcastedo.nosatenda.test.model.productoservice.productoservicetest.cleandb(productoservicetest.java:52)< p>

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/ nosat -spring-config”。“nosaTenda-spring-config xml”。“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部分添加了一些过滤,也许我做错了什么,这就是问题所在。

https://dl.dropboxusercontent.com/u/2635926/pom.xml

https://dl.dropboxusercontent.com/u/2635926/pom.xml

2 个解决方案

#1


0  

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/测试/资源中读取。

So, check where your test application context is, and put a copy of the xml there.

因此,检查您的测试应用程序上下文在哪里,并将xml的副本放在那里。

#2


0  

Ok, I found the solution, it was somewhere else.

我找到了答案,它在别的地方。

I was using a Run Configuration with the forkMode none parameter, as I was running a single test class. forkMode apparently is deprecated and was causing the problem. I replaced it with forkCount 0 and 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.

谢谢。

#1


0  

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/测试/资源中读取。

So, check where your test application context is, and put a copy of the xml there.

因此,检查您的测试应用程序上下文在哪里,并将xml的副本放在那里。

#2


0  

Ok, I found the solution, it was somewhere else.

我找到了答案,它在别的地方。

I was using a Run Configuration with the forkMode none parameter, as I was running a single test class. forkMode apparently is deprecated and was causing the problem. I replaced it with forkCount 0 and 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.

谢谢。