@Configuration
@ComponentScan
public class CDPlayerConfig {
}
将@ComponentScan移除
代码就会报错 因为这样就不会去自动扫描包中的Bean
要在JavaConfig中声明bean,我们需要编写一个方法,这个方法会创建所需类型的实例,然
后给这个方法添加@Bean注解。比方说,下面的代码声明了CompactDisc bean:
package soundsystem;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CDPlayerConfig {
@Bean
public CDplayer cdplayer( ){
return new CDplayer( );
}
@Bean
public CDMusic cdMusic(){
return new CDMusic();
}
}