一、@Autowired和@Qualifier注解
1、@Autowired
默认是根据类型进行注入的,因此如果有多个类型一样的Bean候选者,则需要限定其中一个候选者,否则将抛出异常;
2、@Qualifuer
限定描述符除了能根据名字进行注入,更能进行更细粒度的控制如何选候选者
二、@Autowired的使用
- @Autowired可以对成员变量、方法以及构造函数进行注释
- @Autowired注入bean,相当于在配置文件中配置bean,并且使用setter注入;而对构造函数进行注释,就相当于是使用构造函数进行依赖注入了
(1)推荐使用构造函数进行注释
/**
* 这样作会出现警告:
* Spring Team recommends “Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies”.
* 翻译:总是在您的bean中使用构造函数建立依赖注入,总是使用断言强制依赖
*/
@Autowired
private Service service;
建议后写成下面的样子:
private final Service service
@Autowired
public Controller(Service service){
this.service=service;
}
(2)@Autowired和构造方法执行的顺序解析
@Autowired
private User user;
private String school;
public UserAccountServiceImpl(){
this.school = user.getSchool();
}
以上代码不能运行成功,因为java类会先执行构造方法,然后再给注解了@Autowired的user注入值,所以在执行构造方法的时候就会报错:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘...‘ defined in file [....class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [...]: Constructor threw exception; nested exception is java.lang.NullPointerException
报错信息说:创建Bean时出错,出错原因是实例化bean失败,因为bean时构造方法出错,在构造方法里抛出了空指针异常。
解决办法是使用构造器注入:
private User user;
private String school;
@Autowired
public UserAccountServiceImpl(User user){
this.user = user;
this.school = user.getSchool();
}
可以看出,使用构造器注入的方法可以明确成员变量加载的顺序
注:java变量的初始化顺序为:静态变量或静态语句块 -> 实例变量或初始化语句块 -> 构造方法 -> @Autowired