@SpringApplicationConfiguration和@ContextConfiguration之间的区别

Pri*_*ney 6 java configuration junit spring

之间有什么区别@SpringApplicationConfiguration@ContextConfiguration相对于JUnit测试案例?

dun*_*nni 5

@ContextConfiguration是Spring Test Framework的注释,适用于每个Spring应用程序,@SpringApplicationConfiguration是Spring Boot的注释,实际上是一个复合注释,包括作为加载程序ContextConfiguration的定制SpringApplicationContextLoader


Sky*_*ker 5

@ContextConfiguration并且@SpringApplicationConfiguration两者都做相同的。加载和配置ApplicationContext进行集成测试。但是@ContextConfiguration有些缺乏支持。

@ContextConfiguration支持的资源类型

在之前Spring 3.1,仅支持基于路径的资源位置(通常是XML配置文件)。从Spring 3.1开始,上下文加载器可以选择支持基于路径的资源或基于类的资源。

从Spring 4.0.4开始,上下文加载器可以选择同时支持基于路径的资源和基于类的资源。因此,@ContextConfiguration可用于声明基于路径的资源位置(通过locations()或value()属性)或带注释的类(通过classes()属性)。

但是请注意,SmartContextLoader的大多数实现仅支持一种资源类型。从Spring 4.1开始,基于路径的资源位置可以是XML配置文件或Groovy脚本(如果Groovy在类路径上)。当然,第三方框架可以选择支持其他类型的基于路径的资源。

@SpringApplicationConfiguration与标准相似,@ContextConfiguration使用Spring Boot的SpringApplicationContextLoader

资源链接:

  1. 注释类型SpringApplicationConfiguration
  2. 注释类型ContextConfiguration