原文地址:https://www.cnblogs.com/bihanghang/p/10023759.html

@ContextConfiguration这个注解通常与@RunWith(SpringJUnit4ClassRunner.class)联合使用用来测试

当一个类添加了注解@Component,那么他就自动变成了一个bean,就不需要再Spring配置文件中显示的配置了。把这些bean收集起来通常有两种方式,Java的方式和XML的方式。当这些bean收集起来之后,当我们想要在某个测试类使用@Autowired注解来引入这些收集起来的bean时,只需要给这个测试类添加@ContextConfiguration注解来标注我们想要导入这个测试类的某些bean。

XML

我们先看看老年人使用的XML方式:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd > <!-- 自动扫描该包 -->
<context:component-scan base-package="com" />
</beans>

这个XML文件通过<context:component-scan base-package="com" />标签将com包下的bean全都自动扫描进来。

下面我们就可以测试了。

一般这么写:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/*.xml"})
public class CDPlayerTest { }

@ContextConfiguration括号里的locations = {"classpath*:/*.xml"}就表示将class路径里的所有.xml文件都包括进来,那么刚刚创建的那么XML文件就会包括进来,那么里面自动扫描的bean就都可以拿到了,此时就可以在测试类中使用@Autowired注解来获取之前自动扫描包下的所有bean

classpath和classpath*区别:

  • classpath:只会到你的class路径中查找找文件。

  • classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找。

Java

如果使用Java的方式就会很简单了,我们就不用写XML那么繁琐的文件了,我们可以创建一个Java类来代替XML文件,只需要在这个类上面加上@Configuration注解,然后再加上@ComponentScan注解就开启了自动扫描,如果注解没有写括号里面的东西,@ComponentScan默认会扫描与配置类相同的包。

@Configuration
@ComponentScan
public class CDPlayConfig { }

此时如果想要测试的话,就可以这么写:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayConfig.class)
public class CDPlayerTest { }

相较于XML,是不是很酷炫,这也是官方提倡的方式。

在Spring Boot测试

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class Test {
}

这个@SpringBootTest注解意思就是将SpringBoot主类中导入的bean全都包含进来。

此时SpringBoot主类也被当作了bean的收集器。类似上文的CDPlayConfig。

@SpringBootApplication
@SpringBootConfiguration
@ComponentScan(basePackages = {"com.bihang.*"})
public class CarOrderWebApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(CarOrderWebApplication.class);
} public static void main(String[] args) {
SpringApplication.run(CarOrderWebApplication.class, args);
} }

Spring @ContextConfiguration注解的更多相关文章

  1. mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类

    相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...

  2. Spring _day02_IoC注解开发入门

    1.Spring IoC注解开发入门 1.1 注解开发案例: 创建项目所需要的jar,四个基本的包(beans core context expression ),以及两个日志记录的包,还要AOP的包 ...

  3. Spring 各种注解备注

    Spring 各种注解备注 felix_feng 关注 2016.12.28 10:34* 字数 2092 阅读 790评论 0喜欢 6 转载 (http://blog.csdn.net/sudilu ...

  4. Spring框架系列(七)--Spring常用注解

    Spring部分: 1.声明bean的注解: @Component:组件,没有明确的角色 @Service:在业务逻辑层使用(service层) @Repository:在数据访问层使用(dao层) ...

  5. commons-dbutils实现增删改查(spring新注解)

    1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  6. Spring基于注解自动装配

    前面我们介绍Spring IoC装载的时候,使用XML配置这种方法来装配Bean,这种方法可以很直观的看到每个Bean的依赖,但缺点也很明显:写起来非常繁琐,每增加一个组件,就必须把新的Bean配置到 ...

  7. Spring MVC注解的一些案列

    1.  spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...

  8. Spring系列之Spring常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  9. spring @condition 注解

    spring @condition注解是用来在不同条件下注入不同实现的 demo如下: package com.foreveross.service.weixin.test.condition; im ...

随机推荐

  1. Spring Boot自定义配置实现IDE自动提示

    一.背景 官方提供的spring boot starter的配置项,我们用IDE配置的时候一般都有自动提示的,如下图所示 而我们自己自定义的配置却没有,对开发非常不友好容易打错配置,那这个是怎样实现的 ...

  2. 【Python】itertools之product函数

    [转载]源博客 product 用于求多个可迭代对象的笛卡尔积(Cartesian Product),它跟嵌套的 for 循环等价.即: product(A, B) 和 ((x,y) for x in ...

  3. APS.NET MVC + EF (06)---模型

    在实际开发中,模型往往被划分为视图模型和业务模型两部分,视图模型靠近视图,业务模型靠近业务,但是在具体编码上,它们之间并不是隔离的. 6.1 视图模型和业务模型 模型大多数时候都是用来传递数据的.然而 ...

  4. python——Tkinter图形化界面及threading多线程

    Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口.Tk和Tkinter可以在大多数的Unix平台下使用,同样可以应用在Windows和Macinto ...

  5. Lucene queryParser和analysis有什么不同?

    在Lucene1.4.3中,queryParser和analysis分成独立的两个包,queryParser作用是解析查询语句,analysis作用是分词,queryParser在解析查询语句的时候会 ...

  6. Docker Desktop for Windows 安装步骤

    Docker Desktop for Windows 安装要求 Docker Desktop for Windows需要运行Microsoft Hyper-V.如果需要,Docker Desktop ...

  7. sublime_text运行python ctrl+b运行的界面隐藏了怎么重新调出来恢复显示?

    sublime_text运行python ctrl+b运行的界面隐藏了怎么重新调出来恢复显示?搜索了下都是说怎么隐藏的,隐藏后怎么恢复显示的没找到看进程还在运行,但调不出来看运行结果了,console ...

  8. Angular i18n(国际化方案)

    一.引言 i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.在资讯领域,国际化(i18n)指让产品(出版物,软件,硬件等)无 ...

  9. 搞NDK开发

    1.哪些场景下要用到NDK开发? 跨平台的库,如FFmpeg, skip,weex, 加固,防逆向 签名校验 图片压缩 音视频解码 OpenGL ES 高级特效 热修复 andfix 人脸识别 fac ...

  10. [TCP/IP]TCP服务端accept发生在三次握手的哪一个阶段

    TCP服务端accept发生在三次握手之后 客户端socket()==>connect()==>write()==>read()服务端socket()==>bind()==&g ...