无法加载 Spring ApplicationContext

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18495914/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:11:28  来源:igfitidea点击:

Failed to load Spring ApplicationContext

springmavenjunitspring-aopapplicationcontext

提问by Edmond

I'm writing unit tests for a spring application which is sort of complex. I want to load spring context in order to use defined beans. My context.xml is located at:

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

src/main/resources/context.xml

After maven build, the context.xml appears at:

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

target/classes/context.xml

In the pom.xml, I have: (As suggested by this post)

在 pom.xml 中,我有:(正如这篇文章所建议的)

<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>

I have tried to do this job in two ways:

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

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;
    ...
}

But neither way works for me. Error message:

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

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.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.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access
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
0(ParentRunner.java:42) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) ... more

Important point:The context.xml was actually copied from another project. These projects run together as an application but when function as Junit, I don't know how to load the context from a different project so I simply copied and pasted the file. This might be a problem

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

More informationFailure Trace:

更多信息故障跟踪:

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

So looks like I have to import some spring aop package first?

所以看起来我必须先导入一些spring aop包?

Please give suggestions. Much appreciation.

请给出建议。非常欣赏。

回答by Dhanush Gopinath

When running as junit test using Spring Test annotations, you need to use classpathin the locations like this

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

##代码##

And I don't use any resourcesdefinition in the pom.xml. You can remove that and try like this.

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