我刚开始学习Spring框架(我使用的版本是4.3.0),我认为我们需要使用@Autowired来告诉框架一个类需要注入。
然而,今天我尝试了这个:
@Component
public class CDPlayer implements MediaPlayer{
private CompactDisc cd;
//there are no @Autowired here
public CDPlayer(CompactDisc cd) {
this.cd = cd;
}
public void play() {
cd.play();
}
}
而且它与自动装配配置完美地工作:
@Configuration
@ComponentScan
public class CDPlayerConfigAuto {
}
那么,我什么时候真正需要使用@Autowired注解?