简体   繁体   English

Spring Application Context无法加载配置文件

[英]Spring Application Context can't load the configuration files

I got 2 projects. 我有2个项目。 A plugin project containing some components (POJOs) and a fragment project containing the according unit and integration tests. 一个包含一些组件(POJO)的插件项目,一个包含相应单元测试和集成测试的片段项目。 I'm using Tycho to build these projects and I want to use Spring to bootstrap my integration tests. 我正在使用Tycho来构建这些项目,并且我想使用Spring来引导集成测试。

I've annotated my test classes with 我已经用注释了我的测试课

@ContextConfiguration(locations = { "classpath*:spring/*-config.xml" })
@RunWith(SpringJUnit4ClassRunner.class)

But when I try to build the projects with tycho ( clean install ) or run the test class as Plugin-Test within eclipse, Spring complains that there are no beans in the context defined. 但是,当我尝试使用tycho( clean install )构建项目或在eclipse中以Plugin-Test运行测试类时,Spring抱怨定义的上下文中没有bean。 In the log I found the following lines: 在日志中,我发现了以下几行:

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]

I've put the configuration files under src/main/java/spring/ and src/main/resources/spring but spring can't find them. 我将配置文件放在src/main/java/spring/src/main/resources/spring但是spring找不到它们。 I've also tried to add these paths explicit to the bundle-classpath in the manifest. 我还尝试将这些路径显式添加到清单中的bundle-classpath中。

When I change the configuration path to "file:spring/some-config.xml" spring is loading my bean definitions but crashes when it tries to load the "context" schema with the following output: 当我将配置路径更改为"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]

Why is it not working with the classpath prefix? 为什么它不能与classpath前缀一起使用? And why is it working with the file prefix? 为何使用文件前缀? I thought the file prefix would only work for the file system and not for a jar file... What am I doing wrong? 我以为文件前缀仅适用于文件系统,不适用于jar文件...我在做什么错?

Thanks in advance 提前致谢

Update: Here is a complete view of the (fragment) test project: 更新:这是(片段)测试项目的完整视图:

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

After tycho has tried to execute my test class I see the following files under target: 在tycho尝试执行我的测试类之后,我在target下看到以下文件:

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

I've ommitted the properties and surfire files. 我省略了属性和surfire文件。 The generated config.ini under target/work/configuration/ lists all bundles that are mentioned in the manifest as required bundles. 在target / work / configuration /下生成的config.ini将清单中提到的所有包列为必需的包。 They are referenced as jar files except of my test fragment bundle. 除了我的测试片段捆绑包外,它们都被称为jar文件。 For the test bundle the following entry exists: 对于测试包,存在以下条目:

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

Is this correct? 这个对吗? It would at least explain why the file prefix is working... But what about the classpath prefix? 至少可以解释为什么文件前缀起作用了...但是类路径前缀呢? Has the manifest been copied to the right location in the target folder? 清单是否已复制到目标文件夹中的正确位置? I mean it's outside of the classes folder that is referenced in the dev.properties . 我的意思是,它位于dev.properties引用的classes文件夹dev.properties
Furthermore log4j complains at startup that it's not properly configured which indicates that it can't find the log4j.properties on the classpath. 此外,log4j在启动时会抱怨它配置不正确,这表明它无法在类路径上找到log4j.properties。

Update: Now I'm trying another way. 更新:现在我正在尝试另一种方式。 I've read this article and it seemed to be an easier way to get things running. 我已经阅读了这篇文章 ,这似乎是使事情运行起来更简单的方法。 So I've added the maven-surfire-plugin to my pom and changed my packaging type from "eclipse-test-plugin" to "jar" so that tycho doesn't run it's own surefire-plugin. 因此,我已将maven-surfire-plugin添加到pom中,并将包装类型从“ eclipse-test-plugin”更改为“ jar”,以使tycho不运行它自己的surefire-plugin。 But now I'm standing in front of another problem. 但是现在我站在另一个问题的前面。 Spring seems to provide only an ArtifactLocator for maven2 repositories and not for p2 repositories like tycho uses. Spring似乎仅为maven2存储库提供ArtifactLocator,而不为tycho用途提供p2存储库。
Does anyone know if there is an ArtifactLocator for p2 repositories out there? 有谁知道那里是否有ArtifactLocator用于p2存储库?

Is anyone using the same setup with tycho, osgi and spring for integration testing? 是否有人将tycho,osgi和spring使用相同的设置进行集成测试?

Put spring-context-xx.jar on your classpath. spring-context-xx.jar放在您的类路径上。

Namespaces are handled by implementations of the NamespaceHandler interface. 命名空间由NamespaceHandler接口的实现处理。 At startup spring loads all of them, and attempts to parse each namespace with the loaded handlers. 在启动时,spring会全部加载它们,并尝试使用加载的处理程序解析每个名称空间。 If none of them claims to be able to parse it, the exception is thrown. 如果它们都不声称能够解析它,则抛出异常。 the context: namespace is parsed by ContextNamespaceHandler , which resides in the aforementioned jar. context:名称空间由ContextNamespaceHandler解析,该名称位于上述jar中。

Using Tycho, according to http://blog.vogella.com/2010/07/06/reading-resources-from-plugin/ I tried locations like: 根据http://blog.vogella.com/2010/07/06/reading-resources-from-plugin/ ,使用Tycho我尝试过以下位置:

"platform:/plugin/<host-bundle-id>/<path-to-resource>"

and now its able to load the context configurations as a resource. 现在它能够将上下文配置作为资源加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM