Spring Application Context无法加载配置文件

时间:2011-10-16 20:39:33

标签: java spring unit-testing osgi integration-testing

我有2个项目。包含一些组件(POJO)的插件项目和包含相应单元和集成测试的片段项目。我正在使用Tycho来构建这些项目,我想使用Spring来引导我的集成测试。

我用

注释了我的测试类
@ContextConfiguration(locations = { "classpath*:spring/*-config.xml" })
@RunWith(SpringJUnit4ClassRunner.class)

但是当我尝试使用tycho(clean install)构建项目或者在eclipse中将测试类作为Plugin-Test运行时,Spring抱怨在定义的上下文中没有bean。在日志中我找到了以下几行:

DEBUG o.s.t.c.s.AbstractGenericContextLoader - Loading ApplicationContext for 
      locations [classpath*:spring/*-config.xml].
DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 0 bean definitions from 
      location pattern [classpath*:spring/*-config.xml]

我已将配置文件放在src/main/java/spring/src/main/resources/spring下,但Spring无法找到它们。我还尝试将这些路径显式添加到清单中的bundle-classpath。

当我将配置路径更改为"file:spring/some-config.xml"时,spring正在加载我的bean定义,但在尝试使用以下输出加载“context”模式时崩溃:

Configuration problem: Unable to locate Spring NamespaceHandler for XML schema
namespace [http://www.springframework.org/schema/context]

为什么不使用classpath前缀?为什么它使用文件前缀?我认为文件前缀只适用于文件系统,而不适用于jar文件......我做错了什么?

提前致谢

更新:以下是(片段)测试项目的完整视图:

/
+-- src/main/java/
|   +-- MyTestClass.java
|
+-- src/main/resources/
|   +-- spring/
|   |   +-- some-config.xml
|   +-- log4j.properties
|
+-- META-INF/
|   +-- MANIFEST.MF
|
+-- pom.xml

在tycho尝试执行我的测试类后,我在目标下看到以下文件:

/target
|
+-- classes/
    +-- MyTestClass.class
    +-- spring/
        +-- some-config.xml
    +-- log4j.properties
+-- work/   // contains the eclipse configuration (config.ini, etc.)
+-- MANIFEST.MF
+-- mybundle-xx.jar

我已经省略了属性和冲浪文件。 target / work / configuration /下生成的config.ini将清单中提到的所有bundle列为必需的bundle。除了我的测试片段包之外,它们被引用为jar文件。对于测试包,存在以下条目:

reference\:file\:C\:/[...]/workspaces/workspace/my.bundle.tests

这是对的吗?它至少可以解释文件前缀工作的原因...... 但是类路径前缀怎么样?清单是否已复制到目标文件夹中的正确位置?我的意思是它在classes中引用的dev.properties文件夹之外 此外,log4j在启动时抱怨它没有正确配置,这表明它无法在类路径上找到log4j.properties。

更新:现在我正在尝试另一种方式。我读过this article,这似乎是让事情顺利进行的一种简单方法。所以我已经将maven-surfire-plugin添加到我的pom中,并将我的包装类型从“eclipse-test-plugin”改为“jar”,这样tycho就不会运行它自己的surefire-plugin。但现在我站在另一个问题面前。 Spring似乎只为maven2存储库提供了ArtifactLocator,而不是像tycho一样为p2存储库提供。
有谁知道那里有p2存储库的ArtifactLocator吗?

是否有人使用与tycho,osgi和spring相同的设置进行集成测试?

2 个答案:

答案 0 :(得分:1)

spring-context-xx.jar放在类路径上。

命名空间由NamespaceHandler接口的实现处理。在启动时,spring加载所有这些,并尝试使用加载的处理程序解析每个命名空间。如果他们都没有声称能够解析它,则抛出异常。 context:命名空间由ContextNamespaceHandler解析,它位于前面提到的jar中。

答案 1 :(得分:0)

使用第谷,   根据{{​​3}} 我试过像:

这样的地方
"platform:/plugin/<host-bundle-id>/<path-to-resource>"

现在它能够将上下文配置作为资源加载。