我正在阅读 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?