@ContextConfiguration的意思
@ContextConfiguration的意思
@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);
}
}
@ContextConfiguration的意思的更多相关文章
- 使用@ContextConfiguration注解后,提示找不到配置文件
intellij提示找不到配置文件 错误代码如下: 严重: Caught exception ] java.lang.IllegalStateException: Failed to load App ...
- 【@ContextConfiguration】java世界的那些注解
@ContextConfiguration Spring整合JUnit4测试时,使用注解引入多个配置文件 单个文件 @ContextConfiguration(Locations="../a ...
- @ContextConfiguration注解说明
@ContextConfiguration Spring整合JUnit4测试时,使用注解引入多个配置文件单个文件 @ContextConfiguration(Locations="../ap ...
- (转)@ContextConfiguration注解说明
场景:学习spring实战中相关的单元测试 1 正常使用 @ContextConfiguration Spring整合JUnit4测试时,使用注解引入多个配置文件 1.1 单个文件 @ContextC ...
- java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
写了一个单元测试test来启动activiti,controller放在src/main/java根目录下,test对应也放在src/test/java下,结果报错: java.lang.Illega ...
- spring boot Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Context ...
- maven打包错误:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.118 sec <<< FAILURE! - in ...
- 使注解@ContextConfiguration同时支持locations和classes
@Configuration @ImportResource("classpath:META-INF/dataContext.xml") class TestConfig { } ...
- springboot测试启动报错java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
springboot测试启动报错: java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you ne ...
随机推荐
- JQuery Mobile - 自定义图标!
环境 jquery1.9.1.js jquery mobile 1.4.2版 图标制作 18x18px 底透明的png图标 使用图标,这个过程开始怎么弄都不行,只有个灰色的圆出现,不见图标:或者出现图 ...
- BZOJ 1012--[JSOI2008]最大数maxnumber(二分&单调栈)
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 14142 Solved: 6049[Subm ...
- [JavaScript] 根据指定宽度截取字符串
/** * 根据指定宽度截取字符串 * @param desc 原始字符串 * @param width 该显示的宽度 * @param fontsize 字体大小 12px * @returns { ...
- cAdvisor+InfluxDB+Grafana 监控Docker
容器的监控方案其实有很多,有docker自身的docker stats命令.有Scout.有Data Dog等等,本文主要和大家分享一下比较经典的容器开源监控方案组合:cAdvisor+InfluxD ...
- 这两周服务器被攻击,封锁了600多个IP地址段后今天服务器安静多了
这两周服务器被攻击,封锁了600多个IP地址段后今天服务器安静多了 建议大家在自己的服务器上也封杀这些瘪三的地址 iptables -I INPUT -s 123.44.55.0/24 -j DROP ...
- spring boot + mybatis + druid + redis
接上篇,使用redis做缓存 新建spring boot 工程,添加pom引用 <dependency> <groupId>org.springframework.boot&l ...
- 通俗易懂详解Java代理及代码实战
一.概述 代理模式是Java常用的设计模式之一,实现代理模式要求代理类和委托类(被代理的类)具有相同的方法(提供相同的服务),代理类对象自身并不实现真正的核心逻辑,而是通过调用委托类对象的相关方法来处 ...
- JavaScript -- Navigator
-----040-Navigator.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=&qu ...
- 利用cygwin创建windows下的crontab定时任务
要求 必备知识 熟悉基本编程环境搭建. 运行环境 windows 7(64位); Cygwin-1.7.35 下载地址 环境下载 什么是Cygwin Cygwin是一个在windows平台上运行的类U ...
- What does -> do in clojure?
https://stackoverflow.com/questions/4579226/what-does-do-in-clojure