【问题标题】:Failed to load Spring ApplicationContext无法加载 Spring ApplicationContext
【发布时间】:2013-08-28 18:48:48
【问题描述】:

我正在为一个复杂的 Spring 应用程序编写单元测试。我想加载 spring 上下文以使用定义的 bean。我的 context.xml 位于:

src/main/resources/context.xml

maven构建后,context.xml出现在:

target/classes/context.xml

在 pom.xml 中,我有:(正如 this post 所建议的)

<resources>
    <resource>
        <filtering>true</filtering>
        <directory>src/test/resources</directory>
        <includes>
            <include>**/*.properties</include>
        </includes>
        <excludes>
            <exclude>**/*local.properties</exclude>
        </excludes>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
    </resource>
</resources>

我尝试通过两种方式完成这项工作:

1, Use ApplicationContext
AppliactionContext springContext = new ClassPathXmlApplicationContext("/context.xml");
MyObject myObject = springContext.getBean(myObject.class);

2, Use Annotations
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/context.xml"})
public class myTest{
    @Autowired
    MyObject myObject;
    ...
}

但这两种方法都不适合我。错误信息:

java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
...  more

要点: context.xml 实际上是从另一个项目中复制的。这些项目作为应用程序一起运行,但是当作为 Junit 运行时,我不知道如何从不同的项目加载上下文,所以我只是复制并粘贴了文件。这可能是个问题

更多信息 故障跟踪:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'validator' defined in class path resource [context.xml]: BeanPostProcessor
before instantiation of bean failed; nested exception is    
java.lang.NoClassDefFoundError: org.aspectj.lang.annotation.Aspect

Caused by: java.lang.NoClassDefFoundError: org.aspectj.lang.annotation.Aspect

Caused by: java.lang.ClassNotFoundException: org.aspectj.lang.annotation.Aspect

看来我得先导入一些spring aop包?

请提出建议。非常感谢。

【问题讨论】:

    标签: spring maven junit spring-aop applicationcontext


    【解决方案1】:

    当使用 Spring Test 注解作为 junit 测试运行时,您需要在这样的位置使用 classpath

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:/context.xml")
    

    而且我没有在 pom.xml 中使用任何 resources 定义。您可以删除它并像这样尝试。

    【讨论】:

    • 你能分享你的 context.xml 吗?
    • 对不起。这是保密的。问题可能是 context.xml 实际上不属于这个项目。我将它复制到 src/main/resources。我会在问题中澄清它。
    • 您复制的上下文可能正在加载不在您的类路径中的类和其他上下文xml,这也可能导致无法加载ApplicationContext错误
    • 新错误表明您的类路径中没有 Spring Aspect jar。请将它们添加到您的 pom.xml 并重试
    • 是的,这就是问题所在。我应该更仔细地阅读错误跟踪。感谢您的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 2013-01-27
    • 2017-10-04
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 2020-09-26
    相关资源
    最近更新 更多