@ContextConfiguration 怎么添加配置类

来源:7-1 Spring与JUnit4整合

只能卷了

2023-03-07 18:54:51

问题描述:

@ContextConfiguration(locations = {"classpath:applicationContext.xml"}) 是配置文件。

1.前面我们还学了配置类比如之前的Config,要怎么配置呢?

2.前面我们还学了注解形式,xml里面只是简单的写了一个context:component-scan base-package="com.imooc",那么我们也只需要使用locations引入applicationContext.xml吗?


(谢谢)

写回答

1回答

好帮手慕小蓝

2023-03-08

同学你好~

1.@ContextConfiguration中使用配置的写法是:@ContextConfiguration(classes = {配置类1.class,配置类2.class})

2.注解形式要求必须在配置文件中声明自动扫描,即同学描述的<context:component-scan>配置,而该配置是写在配置文件中的,当使用JUnit测试时,是需要加载配置文件的,否则使用注解的对象无法被自动创建。所以确实只需要使用locations引入applicationContext.xml即可。

祝学习愉快~

1

0 学习 · 9886 问题

查看课程