Spring自动装配

1年前 (2024-04-26)
我们把 Spring 在 Bean 与 Bean 之间建立依赖关系的行为称为“装配”。

Spring 的 IOC 容器虽然功能强大,但它本身不过只是一个空壳而已,它自己并不能独自完成装配工作。需要我们主动将 Bean 放进去,并告诉它 Bean 和 Bean 之间的依赖关系,它才能按照我们的要求完成装配工作。

在前面的学习中,我们都是在 XML 配置中通过 <constructor-arg>和 <property> 中的 ref 属性,手动维护 Bean 与 Bean 之间的依赖关系的。

例如,一个部门(Dept)可以有多个员工(Employee),而一个员工只可能属于某一个部门,这种关联关系定义在 XML 配置的 Bean 定义中,形式如下。

<beans xmlns="http://www.springframework网站站点" rel="nofollow" />

<beans xmlns="http://www.springframework网站站点" rel="nofollow" />

package net.biancheng.c;

public class Dept {

//部门编号

private String deptNo;

//部门名称

private String deptName;

public Dept() {

System.out.println("正在执行 Dept 的无参构造方法>>>>");

}

public Dept(String deptNo, String deptName) {

System.out.println("正在执行 Dept 的有参构造方法>>>>");

this.deptNo = deptNo;

this.deptName = deptName;

}

public void setDeptNo(String deptNo) {

System.out.println("正在执行 Dept 的 setDeptNo 方法>>>>");

this.deptNo = deptNo;

}

public void setDeptName(String deptName) {

System.out.println("正在执行 Dept 的 setDeptName 方法>>>>");

this.deptName = deptName;

}

public String getDeptNo() {

return deptNo;

}

public String getDeptName() {

return deptName;

}

@Override

public String toString() {

return "Dept{" +

"deptNo='" + deptNo + '\'' +

", deptName='" + deptName + '\'' +

'}';

}

}


2.  在 net.biancheng.c 包下,创建一个名为 Employee 的类,代码如下。

package net.biancheng.c;

public class Employee {

//员工编号

private String empNo;

//员工姓名

private String empName;

//部门信息

private Dept dept;

public Employee() {

System.out.println("正在执行 Employee 的无参构造方法>>>>");

}

public Employee(String empNo, String empName, Dept dept) {

System.out.println("正在执行 Employee 的有参构造方法>>>>");

this.empNo = empNo;

this.empName = empName;

this.dept = dept;

}

public void setEmpNo(String empNo) {

System.out.println("正在执行 Employee 的 setEmpNo 方法>>>>");

this.empNo = empNo;

}

public void setEmpName(String empName) {

System.out.println("正在执行 Employee 的 setEmpName 方法>>>>");

this.empName = empName;

}

public void setDept(Dept dept) {

System.out.println("正在执行 Employee 的 setDept 方法>>>>");

this.dept = dept;

}

public Dept getDept() {

return dept;

}

public String getEmpNo() {

return empNo;

}

public String getEmpName() {

return empName;

}

@Override

public String toString() {

return "Employee{" +

"empNo='" + empNo + '\'' +

", empName='" + empName + '\'' +

", dept=" + dept +

'}';

}

}


3. 在 net.biancheng.c 包下,创建一个名为 MainApp 的类,代码如下。

package net.biancheng.c;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

Employee employee = context.getBean("employee", Employee.class);

System.out.println(employee);

}

}

1. 不使用自动装配(autowire="no")

autowire="no" 表示不使用自动装配,此时我们必须通过 <bean> 元素的 <constructor-arg>和 <property> 元素的 ref 属性维护 Bean 的依赖关系。

Beans.xml 配置文件如下。

<beans xmlns="http://www.springframework网站站点" rel="nofollow" />

正在执行 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 配置文件内容如下。

<beans xmlns="http://www.springframework网站站点" rel="nofollow" />

正在执行 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,配置如下。

<beans xmlns="http://www.springframework网站站点" rel="nofollow" />

正在执行 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 配置文件内容如下。

<beans xmlns="http://www.springframework网站站点" rel="nofollow" />

正在执行 Dept 的无参构造方法>>>>

正在执行 Dept 的 setDeptNo 方法>>>>

正在执行 Dept 的 setDeptName 方法>>>>

正在执行 Employee 的无参构造方法>>>>

正在执行 Employee 的 setEmpNo 方法>>>>

正在执行 Employee 的 setEmpName 方法>>>>

正在执行 Employee 的 setDept 方法>>>>

Employee{empNo='002', empName='小郭', dept=Dept{deptNo='1', deptName='技术部'}}


如果同时存在多个相同类型的 Bean,则注入失败,并且引发异常。

<beans xmlns="http://www.springframework网站站点" rel="nofollow" />

正在执行 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 配置文件内容如下。

<beans xmlns="http://www.springframework网站站点" rel="nofollow" />

正在执行 Dept 的有参构造方法>>>>

正在执行 Employee 的有参构造方法>>>>

Employee{empNo='002', empName='小郭', dept=Dept{deptNo='1', deptName='技术部'}}

4. 默认的自动装配模式(autowire="default")

默认采用上一级标签 <beans> 设置的自动装配规则(default-autowire)进行装配,Beans.xml 中的配置内容如下。 

<beans xmlns="http://www.springframework网站站点" rel="nofollow" />

正在执行 Dept 的无参构造方法>>>>

正在执行 Dept 的 setDeptNo 方法>>>>

正在执行 Dept 的 setDeptName 方法>>>>

正在执行 Employee 的无参构造方法>>>>

正在执行 Employee 的 setEmpNo 方法>>>>

正在执行 Employee 的 setEmpName 方法>>>>

正在执行 Employee 的 setDept 方法>>>>

Employee{empNo='002', empName='小郭', dept=Dept{deptNo='1', deptName='技术部'}}