了解弹簧@Autowired用法
我正在阅读spring 3.0.x参考文档,以了解Spring Autowired注释:
我无法理解下面的例子。我们是否需要在 XML 中执行某些操作才能使其正常工作?
示例 1
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
示例 2
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
如何自动连接这两个类,实现相同的接口并使用相同的类?
例:
class Red implements Color
class Blue implements Color
class myMainClass{
@Autowired
private Color color;
draw(){
color.design();
}
}
将调用哪种设计方法?如何确保调用 Red 类的设计方法而不是 Blue?