有 cdproperties.properties 文件,内容如下:
disc.title = "SgtPeppers's songs"
disc.artist = "The Beaties"
有一个 SgtPeppers 类,待注入 title 和 artist 属性:
public class SgtPeppers implements CompactDisc {
private String title;
private String artist;
/**
* 用 setter 方法还是构造器来注入都无所谓
* 最好能两种方案都讲一下该如何实现 */
public void play(){
System.out.println("playing" + this.title + "written by" + this.artist);
}
}
在 CDplayerConfig 中
@Configuration
public class CDPlayerConfig {
@Bean
public CompactDisc sgtPeppers(){
/**
* 最终会返回一个 SgtPeppers 类的
* 但如何在这里进行配置,将 cdproperties.properties 中的属性值注入?
* 不允许开启使用自动配置!
*/
}
@Bean
public CDPlayer cdPlayer(CompactDisc CD){
CDPlayer cdPlayer = new CDPlayer();
cdPlayer.setCd(CD);
return cdPlayer;
}
}
谢谢。