使用junit測(cè)試置谦,自動(dòng)裝配
不是junit測(cè)試的話寄啼,會(huì)在web.xml中配置識(shí)別注解的xml
junit使用下面兩種方式識(shí)別注解
先了解junit的2個(gè)注解
spring 集成 junit4 2個(gè)注解
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
單個(gè)配置使用()
@ContextConfiguration()
多個(gè)配置使用{}
@ContextConfiguration{}
里面的配置 classes餐茵、locations
classes=CDPlayerConfig.class
locations="classpath:META-INF/spring/soundsystem.xml" 執(zhí)行測(cè)試前加載xml文件
2種識(shí)別注解
- 使用注解 并在測(cè)試
@Configuration
@ComponentScan
- 使用junit4注解
配置locations 加載xml 并在xml中加入識(shí)別注解的 context:component-scan
<context:component-scan base-package="soundsystem" />
也可以在方法上使用@Autowire獲取bean
@Autowired
private CompactDisc cd;
// @Autowired
// public CDPlayer(CompactDisc cd) {
// this.cd = cd;
// }