答:运用了多态,比如 public class AttrServiceImpl implements AttrService 被注入到容器中
此时AttrServiceImpl的属性有两个,一个是本身,另一个是AttrService。
在Controller(已被注入到容器中)中 private AttrService attrService 使用@Autowired自动注入时,是根据属性来注入的。就会在Spring IOC容器中寻找属性为AttrService的,并自动注入。
因为AttrServiceImpl implements AttrService,和容器的自动创建
Service service = new ServiceImpl;
运行看左 = 编译看右
编译的时候不管右边的类,只要左边的能通过就行,
运行的时候就要按右边的实际方法执行。
所以
@Autowired
private AttrService attrService; 中的是使用实现类的方法。