美文网首页
自动装配bean 2.2节

自动装配bean 2.2节

作者: KeDaiBiaO1 | 来源:发表于2017-11-14 16:28 被阅读0次

使用junit测试,自动装配

  1. 不是junit测试的话,会在web.xml中配置识别注解的xml

  2. junit使用下面两种方式识别注解
    先了解junit的2个注解
    spring 集成 junit4 2个注解
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration

单个配置使用()
@ContextConfiguration()
多个配置使用{}
@ContextConfiguration{}

里面的配置  classes、locations
classes=CDPlayerConfig.class  
locations="classpath:META-INF/spring/soundsystem.xml"   执行测试前加载xml文件

2种识别注解

  1. 使用注解 并在测试
@Configuration
@ComponentScan
  1. 使用junit4注解
    配置locations 加载xml 并在xml中加入识别注解的 context:component-scan
<context:component-scan base-package="soundsystem" />

也可以在方法上使用@Autowire获取bean

  @Autowired
  private CompactDisc cd;

//  @Autowired
//  public CDPlayer(CompactDisc cd) {
//    this.cd = cd;
//  }

相关文章

网友评论

      本文标题:自动装配bean 2.2节

      本文链接:https://www.haomeiwen.com/subject/pbdfvxtx.html