@Configuration
public class CDPlayerConfig {
@Bean
public CompactDisc sgtPeppers(){
return new SgtPeppers();
}
@Bean
public CDPlayer cdPlay(){
return new CDPlayer(sgtPeppers());
}
}
这个是能执行的
把
@Bean
public CompactDisc sgtPeppers(){
return new SgtPeppers();
}
放到另一个配置文件中,然后通过@Import引入
@Configuration
public class CDConfig {
@Bean
public CompactDisc sgtPeppers(){
return new SgtPeppers();
}
}
引入外部的配置文件
@Configuration
@Import(CDConfig.class)
public class CDPlayerConfig {
@Bean
public CDPlayer cdPlay(){
return new CDPlayer(sgtPeppers());
}
}
就找不到这个method
伊谢尔伦2017-04-18 10:51:33
The basic concept is wrong. sgtPeppers() is a method of calling this class. If it is not defined, of course it will cause a compilation error and has nothing to do with spring.
Change it like this:
@Configuration
@Import(CDConfig.class)
public class CDPlayerConfig {
@Bean
public CDPlayer cdPlay(CompactDisc cd){
return new CDPlayer(cd);
}
}