概述

自动装配是Spring满足bean依赖一种方式,Spring会在上下文中自动寻找,并自动给bean装配属性

自动装配有两种方式:

  1. ByName

    根据Setter方法名指定自动装配的bean的id,例如:Setter的方法名为setDog1,所以bean的id就必须为dog1

  2. ByType

    需要保证所有bean的class唯一,即不可以存在相同类型的bean

代码实现

测试环境

一个People有Dog和Cat

手动装配

People实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.ledao.entity;

/**
* @author LeDao
* @company
* @create 2022-02-15 18:13
*/
public class People {

private String name;
private Dog dog;
private Cat cat;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Dog getDog() {
return dog;
}

public void setDog(Dog dog) {
this.dog = dog;
}

public Cat getCat() {
return cat;
}

public void setCat(Cat cat) {
this.cat = cat;
}

@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", dog=" + dog +
", cat=" + cat +
'}';
}
}

Dog实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.ledao.entity;

/**
* @author LeDao
* @company
* @create 2022-02-15 18:14
*/
public class Dog {

private String name;

public Dog() {
System.out.println("狗吃骨头");
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
'}';
}
}

Cat实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.ledao.entity;

/**
* @author LeDao
* @company
* @create 2022-02-15 18:14
*/
public class Cat {

private String name;

public Cat() {
System.out.println("猫吃鱼");
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
'}';
}
}

ApplicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="dog" class="com.ledao.entity.Dog">
<property name="name" value="小花"/>
</bean>

<bean id="cat" class="com.ledao.entity.Cat">
<property name="name" value="小哈"/>
</bean>

<bean id="people" class="com.ledao.entity.People">
<property name="name" value="小明"/>
<property name="dog" ref="dog"/>
<property name="cat" ref="cat"/>
</bean>
</beans>

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import com.ledao.entity.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @author LeDao
* @company
* @create 2022-02-12 15:56
*/
public class MyTest {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
People people = context.getBean("people", People.class);
System.out.println(people);
}
}

结果

image-20220215192042401

自动装配

ByName方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="dog" class="com.ledao.entity.Dog">
<property name="name" value="小哈1"/>
</bean>
<bean id="dog2" class="com.ledao.entity.Dog">
<property name="name" value="小哈2"/>
</bean>
<bean id="cat" class="com.ledao.entity.Cat">
<property name="name" value="小花1"/>
</bean>

<bean id="people" class="com.ledao.entity.People" autowire="byName">
<property name="name" value="tom"/>
</bean>
</beans>

ByType方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="dog1" class="com.ledao.entity.Dog">
<property name="name" value="小哈1"/>
</bean>
<bean id="cat" class="com.ledao.entity.Cat">
<property name="name" value="小花1"/>
</bean>

<bean id="people" class="com.ledao.entity.People" autowire="byType">
<property name="name" value="tom"/>
</bean>
</beans>