I wrote a simple example by use java configuration for start learn Spring framework, but it only passed in unit test, not in main function.
@Component
public class CDPlayer implements MediaPlayer {
private CompactDisc cd;
@Autowired
public CDPlayer(CompactDisc cd) {
this.cd = cd;
}
public void play() {
cd.play();
}
}
// ---------
@Component
public class JayCD implements CompactDisc {
public void play() {
System.out.println("Playing A CD");
}
}
// ---------
@Configuration
@ComponentScan
public class CDPlayerConfig {
}
// ---------
public class Main {
@Autowired
static MediaPlayer mediaPlayer;
@Autowired
static CompactDisc compactDisc;
public static void main(String[] args){
ApplicationContext context = new AnnotationConfigApplicationContext(CDPlayerConfig.class);
mediaPlayer.play();
}
}
mediaPlayer
is NULL??
Why is the use of annotations unsuccessful? How to modify code?
Thanks!