java - 为什么@Import引入一个config不能使用这个config中@Bean注解的方法?
问题描述
@Configurationpublic class CDPlayerConfig { @Bean public CompactDisc sgtPeppers(){return new SgtPeppers(); }@Bean public CDPlayer cdPlay(){return new CDPlayer(sgtPeppers()); }}
这个是能执行的
把
@Beanpublic CompactDisc sgtPeppers(){ return new SgtPeppers();}
放到另一个配置文件中,然后通过@Import引入
@Configurationpublic 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
问题解答
回答1:基本概念错误,sgtPeppers()是调用本类的方法,没有定义当然编译错误,和spring无关。这么改:
@Configuration@Import(CDConfig.class)public class CDPlayerConfig { @Bean public CDPlayer cdPlay(CompactDisc cd){return new CDPlayer(cd); }}回答2:
理解有误
可以使用 @Bean @Qualifier配合
相关文章:
1. 这段代码是获取百度收录量的!需要怎么设置才能获取百度快照旁边的网址呢?2. node.js - nodejs中mysql子查询返回多行结果怎么处理?3. javascript - 很多网页前端都是一整个图片裁剪出来很多小的素材比如按钮图标等这是为什么?手机端开发也会这么操作吗?4. python3.x - python中import theano出错5. python - mysql 如何设置通用型字段? 比如像mongodb那样6. html - echarts如何自定义tooltip7. node.js - 微信的自动回复问题8. javascript - 写移动端的页面的时候,有不一快空白,是怎么回事?9. javascript - 我写的href跳转地址不是百度,为什么在有的机型上跳转到百度了,有的机型跳转正确10. 设置python环境变量时不小心把原始path都删了,请问怎么恢复? win7 64位的