无法加载 Spring ApplicationContext

2024-04-26

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

src/main/resources/context.xml

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

target/classes/context.xml

在 pom.xml 中,我有:(根据建议这个帖子 https://stackoverflow.com/questions/10104372/testing-with-spring-and-maven-applicationcontext)

<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 Test 注解作为 junit 测试运行时,您需要使用classpath在这样的地方

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

而且我不使用任何resourcespom.xml 中的定义。您可以删除它并尝试这样。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法加载 Spring ApplicationContext 的相关文章

随机推荐

  • 安卓浏览器有bug? div溢出滚动

    你能让div的溢出内容在Android浏览器中滚动吗 它在所有其他现代浏览器中都是可滚动的 在 iOS 中 它是可滚动的 但它不显示滚动条 但可以通过拖动来滚动 一个简单的例子 http jsfiddle net KPuW5 1 embed
  • .hide("slow") 是同步方法还是异步方法?

    据我们所知 ajax 是一个异步方法 因为下一个语句之前开始执行ajax 方法已完全执行 ajax 继续并行执行其工作 并且hide 是一个同步方法 因为它立即隐藏元素 并且下一条语句将在何时执行hide 确实完成了他的全部任务 但我真的很
  • 此 xpath 查询的 Flex/AS3/E4X 等效项是什么?

    鉴于此文档
  • 一个应用程序的最大线程数?

    我想知道一个应用程序的最大线程数 您知道 Thread activeCount 返回正在运行的线程组及其子组中的活动线程数 如果我知道当前活动中要创建的最大线程数 我就可以限制活动线程 我正在使用线程进行 http 连接并捕获 Http 响
  • 类通过回调提供数据

    在用 C 编程一段时间后 我最近回到了 Visual C 因为 C 中的回调要容易得多 我有一个控制 0 连接设备的单例类 我的想法是在此类中创建一个函数 它将迭代一组 连接的设备并通过回调将其发布到任何可能需要它的地方 e g Singl
  • C++ 或 Java 中保存 20 位整数的数据类型

    Java 或 C 中是否有可以保存 20 位或更多数字的整数值的数据类型 long long 数据类型最多只能容纳 18 位数字 Java具体 您正在寻找BigInteger http docs oracle com javase 7 do
  • 如何在H2中定义Oracle Package procedure进行测试

    我正在测试一个 Spring Boot 应用程序 该应用程序将数据读取 写入 Oracle DB 该 Oracle DB 具有 Oracle 包以及这些包中的过程 在某些时候 Spring Boot应用程序通过实体存储库调用此过程 如下所示
  • 模拟 with 语句中使用的类

    我有一堂课有一个 exit and enter 函数 以便我可以在 with 语句中使用它 例如 with ClassName as c c do something 我现在正在尝试编写一个单元测试来测试这一点 基本上 我想测试一下do s
  • 运行时静默更新应用程序

    我的应用程序最初将使用 Windows Installer 进行部署 我正在寻找的解决方案的主要特征包括 支持应用程序静默更新 运行 或自动重启 客户 易于维护和管理包装 过程 避免复杂的定制或 安装脚本 您对我如何实现这一目标有什么想法吗
  • 如何测试两个哈希值(密码)是否相似?

    当用户创建密码时 我对其进行哈希处理 包括盐 并将其保存在数据库中 现在 当用户想要更改他或她的密码时 我想测试新密码是否与旧密码太相似 我已经在不同的服务上看到过这种情况 尤其是网上银行 所以 我想我会使用similar text or
  • 更改 DataGridViewComboBoxColumn 单元格的背景颜色

    我创建了一个 DataGridView 对象 其中包含 DataGridViewComboBoxColumn 类型的列 以允许用户从下拉列表中选择值 例如 如果用户选择 高 我想对组合框的背面进行着色 但是 它不会为组合框着色 而只会为组合
  • 如何使用程序集绑定重定向来忽略修订号和内部版本号

    我有几个 C NET 应用程序 以及供它们访问数据库的 API 我想将 API 的所有版本放入数据库中 并让它们选择最高版本和内部版本号 但坚持使用它们构建时使用的主要版本号和次要版本号 基本上当我参考API时1 2 3 4我想要参考阅读1
  • 基于github仓库的本地仓库创建本地git仓库并保持更新

    我有一些基本的 git 知识 但我不知道如何实现这一点 我正在尝试克隆 github WordPress 入门主题下划线 https github com automattic s 这个想法是创建一个基本框架based 经过一些修改 在该存
  • 无法将 PHP 表单放入表格中

    我创建了一个工作正常的表单 但是当我将其放入表格中时 它总是认为该表单不完整 有没有办法可以将其放入表格中 这样看起来不错 而不会出现此问题 我的 HTML h2 class green Interested in making life
  • .Net Core Web Api 异步不重要吗?

    我一直在编写我的 Web Api 控制器方法async关键字并一直在使用async一直往下 我最近尝试创建一个方法同步来看看它会如何影响性能 并惊讶地发现它对任何其他 http 请求都没有阻塞影响 举个例子 Route Foo class
  • aws key 和 Secret 在 aws cli 上工作但在 jenkins 上不起作用

    我正在尝试运行 python 脚本 其中包含使用 boto3 sdk 访问 s3 的代码 我已经在我的 Windows 机器中创建了默认配置文件 aws cli 命令在窗口 cmd 中可以正常工作以访问 s3 存储桶 python 脚本在窗
  • PHP 计算 JSON 请求中返回的项目数?

    我正在寻找一种方法来计算在搜索数据库时得到的这些 JSON 字符串中返回的项目数 以 PHP 表示 请原谅我 因为我对这一切一无所知 有人告诉我 因为 JSON 版本中没有返回计数 就像此数据库中的 XML 版本一样 我必须使用循环来计算结
  • 简单的http服务器

    好吧 这可能是一个愚蠢的问题 但关于节点 我已经尽我所能了 我设置了一个服务器 使用我们可以在任何节点演示或教程中找到的代码 var http require http var server http createServer functi
  • 如何在VueJS中将所有事件传递给父级

    传递道具 In VueJS如果你设置inheritAttrs to false并使用v bind attrs 您将组件中未声明的所有 props 传递给其子组件 是否有类似的方法将来自子级的所有事件传递给其父级VueJS 代码示例 Wrap
  • 无法加载 Spring ApplicationContext

    我正在为一个有点复杂的 spring 应用程序编写单元测试 我想加载 spring 上下文以便使用定义的 bean 我的 context xml 位于 src main resources context xml Maven 构建后 con