功能 :
1、可对类成员变量及方法入参进行标注
@Service
public class LoginService {
@Autowied
private LogDao logDao;
@Autowired
private UserDao userDao;
@Autowired
public void setLogDao(LogDao logDao) {
this.logDao=logDao;
}
@Autowired
@Qualifier("userDao")
public void setUserdao(UserDao userDao) {
this.userDao=userDao;
}
@Autowired
public void init(@Qualifier("userDao") UserDao userDao, LogDao logDao) {}
}
2、对集合类进行标注
public class MyComponent {
@Autowired
private List plugins; //Plugin 是接口
}
如果有Plugin的子实现类p1,p2,p3(都被@Component标注)
注入的时候会将p1,p2,p3作为元素注入到plugins集合中
@Autowired 按类型匹配
@Resource 按名称匹配