我们把 Spring 在 Bean 与 Bean 之间建立依赖关系的行为称为“装配”。
Spring 的 IOC 容器虽然功能强大,但它本身不过只是一个空壳而已,它自己并不能独自完成装配工作。需要我们主动将 Bean 放进去,并告诉它 Bean 和 Bean 之间的依赖关系,它才能按照我们的要求完成装配工作。
在前面的学习中,我们都是在 XML 配置中通过 和 中的 ref 属性,手动维护 Bean 与 Bean 之间的依赖关系的。
例如,一个部门(Dept)可以有多个员工(Employee),而一个员工只可能属于某一个部门,这种关联关系定义在 XML 配置的 Bean 定义中,形式如下。
对于只包含少量 Bean 的应用来说,这种方式已经足够满足我们的需求了。但随着应用的不断发展,容器中包含的 Bean 会越来越多,Bean 和 Bean 之间的依赖关系也越来越复杂,这就使得我们所编写的 XML 配置也越来越复杂,越来越繁琐。
我们知道,过于复杂的 XML 配置不但可读性差,而且编写起来极易出错,严重的降低了开发人员的开发效率。为了解决这一问题,Spring 框架还为我们提供了“自动装配”功能。
Spring 自动装配
Spring 的自动装配功能可以让 Spring 容器依据某种规则(自动装配的规则,有五种),为指定的 Bean 从应用的上下文(AppplicationContext 容器)中查找它所依赖的 Bean,并自动建立 Bean 之间的依赖关系。而这一过程是在完全不使用任何 和 元素 ref 属性的情况下进行的。
Spring 的自动装配功能能够有效地简化 Spring 应用的 XML 配置,因此在配置数量相当多时采用自动装配降低工作量。
Spring 框架式默认不支持自动装配的,要想使用自动装配,则需要对 Spring XML 配置文件中 元素的 autowire 属性进行设置。
<!--雇员 Employee 的 Bean 定义,通过 autowire 属性设置自动装配的规则-->
<bean id="employee" class="net.biancheng.c.Employee" autowire="byName">
</bean>
自动装配规则 Spring 共提供了 5 中自动装配规则,它们分别与 autowire 属性的 5 个取值对应,具体说明如下表。 ![image.png](https://img-blog.csdnimg.cn/img_convert/aa866fdf7873122f0df3172ad5112e74.png) ## 示例
下面,我们就通过一个简单的实例,对 Spring 的自动装配功能进行演示。
1. 参看《[第一个 Spring 程序]》,新建一个名为 my-spring-autowire-demo 的 Java 项目。
2. 在 net.biancheng.c 包下,创建一个名为 Dept 的类,代码如下。
1. package net.biancheng.c;
3. public class Dept {
4. //部门编号
5. private String deptNo;
6. //部门名称
7. private String deptName;
9. public Dept() {
10. System.out.println("正在执行 Dept 的无参构造方法>>>>");
11. }
13. public Dept(String deptNo, String deptName) {
14. System.out.println("正在执行 Dept 的有参构造方法>>>>");
15. this.deptNo = deptNo;
16. this.deptName = deptName;
17. }
19. public void setDeptNo(String deptNo) {
20. System.out.println("正在执行 Dept 的 setDeptNo 方法>>>>");
21. this.deptNo = deptNo;
22. }
24. public void setDeptName(String deptName) {
25. System.out.println("正在执行 Dept 的 setDeptName 方法>>>>");
26. this.deptName = deptName;
27. }
29. public String getDeptNo() {
30. return deptNo;
31. }
33. public String getDeptName() {
34. return deptName;
35. }
37. @Override
38. public String toString() {
39. return "Dept{" +
40. "deptNo='" + deptNo + '\'' +
41. ", deptName='" + deptName + '\'' +
42. '}';
43. }
45. }
2. 在 net.biancheng.c 包下,创建一个名为 Employee 的类,代码如下。
1. package net.biancheng.c;
3. public class Employee {
4. //员工编号
5. private String empNo;
6. //员工姓名
7. private String empName;
8. //部门信息
9. private Dept dept;
11. public Employee() {
12. System.out.println("正在执行 Employee 的无参构造方法>>>>");
13. }
15. public Employee(String empNo, String empName, Dept dept) {
16. System.out.println("正在执行 Employee 的有参构造方法>>>>");
17. this.empNo = empNo;
18. this.empName = empName;
19. this.dept = dept;
20. }
22. public void setEmpNo(String empNo) {
23. System.out.println("正在执行 Employee 的 setEmpNo 方法>>>>");
24. this.empNo = empNo;
25. }
27. public void setEmpName(String empName) {
28. System.out.println("正在执行 Employee 的 setEmpName 方法>>>>");
29. this.empName = empName;
30. }
32. public void setDept(Dept dept) {
33. System.out.println("正在执行 Employee 的 setDept 方法>>>>");
34. this.dept = dept;
35. }
37. public Dept getDept() {
38. return dept;
39. }
41. public String getEmpNo() {
42. return empNo;
43. }
45. public String getEmpName() {
46. return empName;
47. }
49. @Override
50. public String toString() {
51. return "Employee{" +
52. "empNo='" + empNo + '\'' +
53. ", empName='" + empName + '\'' +
54. ", dept=" + dept +
55. '}';
56. }
57. }
3. 在 net.biancheng.c 包下,创建一个名为 MainApp 的类,代码如下。
1. package net.biancheng.c;
3. import org.springframework.context.ApplicationContext;
4. import org.springframework.context.support.ClassPathXmlApplicationContext;
6. public class MainApp {
8. public static void main(String[] args) {
9. ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
10. Employee employee = context.getBean("employee", Employee.class);
11. System.out.println(employee);
12. }
13. }
1. 不使用自动装配(autowire=“no”)
autowire=“no” 表示不使用自动装配,此时我们必须通过 元素的 和 元素的 ref 属性维护 Bean 的依赖关系。
Beans.xml 配置文件如下。
1.
执行 MainApp 的 main() 方法,控制台输出如下。
正在执行 Dept 的无参构造方法>>>> 正在执行 Dept 的 setDeptNo 方法>>>> 正在执行 Dept 的 setDeptName 方法>>>> 正在执行 Employee 的无参构造方法>>>> 正在执行 Employee 的 setEmpNo 方法>>>> 正在执行 Employee 的 setEmpName 方法>>>> 正在执行 Employee 的 setDept 方法>>>> Employee{empNo='002', empName='小郭', dept=Dept{deptNo='1', deptName='技术部'}}
2. 按名称自动装配(autowire=“byName”)
autowire=“byName” 表示按属性名称自动装配,XML 文件中 Bean 的 id 或 name 必须与类中的属性名称相同。
Beans.xml 配置文件内容如下。
1.
执行 MainApp 的 main() 方法,控制台输出如下。
正在执行 Dept 的无参构造方法>>>> 正在执行 Dept 的 setDeptNo 方法>>>> 正在执行 Dept 的 setDeptName 方法>>>> 正在执行 Employee 的无参构造方法>>>> 正在执行 Employee 的 setEmpNo 方法>>>> 正在执行 Employee 的 setEmpName 方法>>>> 正在执行 Employee 的 setDept 方法>>>> Employee{empNo='002', empName='小郭', dept=Dept{deptNo='1', deptName='技术部'}}
在 Beans.xml 中,将员工 Bean 的 id 修改为 dept2,配置如下。
1.
重新执行 MainApp 中 main() 方法,控制台输出如下。
正在执行 Dept 的无参构造方法>>>> 正在执行 Dept 的 setDeptNo 方法>>>> 正在执行 Dept 的 setDeptName 方法>>>> 正在执行 Employee 的无参构造方法>>>> 正在执行 Employee 的 setEmpNo 方法>>>> 正在执行 Employee 的 setEmpName 方法>>>> Employee{empNo='002', empName='小郭', dept=null}
3. 按类型自动装配(autowire=“byType”)
autowire=“byType” 表示按类中对象属性数据类型进行自动装配。即使 XML 文件中 Bean 的 id 或 name 与类中的属性名不同,只要 Bean 的 class 属性值与类中的对象属性的类型相同,就可以完成自动装配。
Beans.xml 配置文件内容如下。
1.
重新执行 MainApp 中 main() 方法,控制台输出如下。
正在执行 Dept 的无参构造方法>>>> 正在执行 Dept 的 setDeptNo 方法>>>> 正在执行 Dept 的 setDeptName 方法>>>> 正在执行 Employee 的无参构造方法>>>> 正在执行 Employee 的 setEmpNo 方法>>>> 正在执行 Employee 的 setEmpName 方法>>>> 正在执行 Employee 的 setDept 方法>>>> Employee{empNo='002', empName='小郭', dept=Dept{deptNo='1', deptName='技术部'}}
如果同时存在多个相同类型的 Bean,则注入失败,并且引发异常。
1.
异常信息如下。
正在执行 Dept 的无参构造方法>>>> 正在执行 Dept 的 setDeptNo 方法>>>> 正在执行 Dept 的 setDeptName 方法>>>> 正在执行 Dept 的无参构造方法>>>> 正在执行 Dept 的 setDeptNo 方法>>>> 正在执行 Dept 的 setDeptName 方法>>>> 正在执行 Employee 的无参构造方法>>>> 十二月 23, 2021 5:14:04 下午 org.springframework.context.support.AbstractApplicationContext refresh 警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employee' defined in class path resource [Beans.xml]: Unsatisfied dependency expressed through bean property 'dept'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'net.biancheng.c.Dept' available: expected single matching bean but found 2: dept2,dept Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employee' defined in class path resource [Beans.xml]: Unsatisfied dependency expressed through bean property 'dept'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'net.biancheng.c.Dept' available: expected single matching bean but found 2: dept2,dept
4. 构造函数自动装配(autowire=“constructor”)
autowire=“constructor” 表示按照 Java 类中构造函数进行自动装配。
Beans.xml 配置文件内容如下。
1.
重新执行 MainApp 中 main() 方法,控制台输出如下。
正在执行 Dept 的有参构造方法>>>> 正在执行 Employee 的有参构造方法>>>> Employee{empNo='002', empName='小郭', dept=Dept{deptNo='1', deptName='技术部'}}
4. 默认的自动装配模式(autowire=“default”)
默认采用上一级标签 设置的自动装配规则(default-autowire)进行装配,Beans.xml 中的配置内容如下。
1.
重新执行 MainApp 中 main() 方法,控制台输出如下。
正在执行 Dept 的无参构造方法>>>> 正在执行 Dept 的 setDeptNo 方法>>>> 正在执行 Dept 的 setDeptName 方法>>>> 正在执行 Employee 的无参构造方法>>>> 正在执行 Employee 的 setEmpNo 方法>>>> 正在执行 Employee 的 setEmpName 方法>>>> 正在执行 Employee 的 setDept 方法>>>> Employee{empNo='002', empName='小郭', dept=Dept{deptNo='1', deptName='技术部'}}