5.Spring给bean的属性赋值(配置文件)

 

在Spring的容器配置文件中给bean的属性赋值

DI(依赖注入)的分类:

  1. 设值注入:调用bean对象的set()方法,在创建对象之后给属性赋值
  2. 构造注入:调用bean对象的构造方法,创建对象的同时给属性赋值

1. 设值注入

1.1 简单类型:String和Java的基本数据类型

创建实体类Student

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Student {

public String name;
public int age;

public void setName(String name) {
System.out.println("setName");
this.name = name;
}

public void setAge(int age) {
System.out.println("setAge");
this.age = age;
}

public void setAddress(String address) {
System.out.println("setAddress");
}

@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
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">

<!-- 设值注入 -->
<!-- 注册Student bean对象 -->
<bean id="myStudent" class="com.node.ba01.Student">
<!-- 给bean的属性赋值,使用设值注入,简单类型的属性使用value
name:bean的属性名
value:简单类型的属性值
-->
<property name="name" value="张三" />
<property name="age" value="20" />
<property name="address" value="北京" />
</bean>

</beans>

1.2 给引用类型的属性赋值

实体类Student

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
public class Student {

private String name;
private int age;

//引用类型的属性
private School school;

public void setSchool(School school) {
this.school = school;
}

public void setName(String name) {
System.out.println("setName");
this.name = name;
}

public void setAge(int age) {
System.out.println("setAge");
this.age = age;
}

public void setAddress(String address) {
System.out.println("setAddress");
}

@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
}
}

创建实体类School

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class School {

private String name;
private String address;

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

@Override
public String toString() {
return "School [name=" + name + ", address=" + address + "]";
}
}
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
<?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">

<!-- 设值注入 -->
<!-- 注册School bean对象 -->
<bean id="mySchool" class="com.node.ba02.School">
<property name="name" value="北京大学" />
<property name="address" value="海淀区" />
</bean>

<!-- 注册Student bean对象 -->
<bean id="myStudent" class="com.node.ba02.Student">
<!-- 给bean的属性赋值,使用设值注入,简单类型的属性使用value
name:bean的属性名
value:简单类型的属性值
-->
<property name="name" value="张三" />
<property name="age" value="20" />
<property name="address" value="北京" />

<!-- 给引用类型的属性赋值,使用ref属性给引用类型的属性赋值 -->
<property name="school" ref="mySchool" />

</bean>

<!-- 使用ref子标签给引用类型的属性赋值 -->
<bean id="myStudent2" class="com.node.ba02.Student">
<property name="name" value="李四" />
<property name="age" value="20" />
<property name="address" value="北京" />

<!-- 给引用类型的属性赋值,使用ref子标签 -->
<property name="school">
<ref bean="mySchool"/>
</property>
</bean>
</beans>

2. 构造注入

实体类Student

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
public class Student {

private String name;
private int age;

//引用类型的属性
private School school;

public Student(String myName, int myAge, School mySchool) {
this.name=myName;
this.age=myAge;
this.school=mySchool;
}

public void setSchool(School school) {
this.school = school;
}

public void setName(String name) {
System.out.println("setName");
this.name = name;
}

public void setAge(int age) {
System.out.println("setAge");
this.age = age;
}

public void setAddress(String address) {
System.out.println("setAddress");
}

@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
}
}
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
<?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的有参数的构造方法,创建对象的同时给属性赋值 -->
<!-- 使用name属性 -->
<bean id="myStudent" class="com.node.ba03.Student">
<!--
name:构造方法的形参名
value:简单类型的属性值
ref:引用类型的属性名
-->
<constructor-arg name="myName" value="张三" />
<constructor-arg name="myAge" value="26" />
<constructor-arg name="mySchool" ref="mySchool" />
</bean>

<!-- 使用index属性,表示参数的索引位置,从0开始 -->
<bean id="myStudent2" class="com.node.ba03.Student">
<constructor-arg index="0" value="张三" />
<constructor-arg index="1" value="28" />
<constructor-arg index="2" ref="mySchool" />
</bean>

<!-- 注册School对象 -->
<bean id="mySchool" class="com.node.ba03.School">
<property name="name" value="实验小学" />
<property name="address" value="海淀区" />
</bean>
</beans>

3. 集合类型的属性赋值

对Array, List, Set, Map, Properties类型的属性赋值

创建实体类MyCollections

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
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class MyCollections {

private String[] myStrs;
private List<Student> myList;
private Set<String> mySet;
private Map<String, Integer> myMap;
private Properties myProps;

//复杂的集合类型
private List<Map<String, String>> myListMap;

public List<Map<String, String>> getMyListMap() {
return myListMap;
}
public void setMyListMap(List<Map<String, String>> myListMap) {
this.myListMap = myListMap;
}
public String[] getMyStrs() {
return myStrs;
}
public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs;
}
public List<Student> getMyList() {
return myList;
}
public void setMyList(List<Student> myList) {
this.myList = myList;
}
public Set<String> getMySet() {
return mySet;
}
public void setMySet(Set<String> mySet) {
this.mySet = mySet;
}
public Map<String, Integer> getMyMap() {
return myMap;
}
public void setMyMap(Map<String, Integer> myMap) {
this.myMap = myMap;
}
public Properties getMyProps() {
return myProps;
}
public void setMyProps(Properties myProps) {
this.myProps = myProps;
}
@Override
public String toString() {
return "MyCollections [myStrs=" + Arrays.toString(myStrs) + ", myList=" + myList + ", mySet=" + mySet
+ ", myMap=" + myMap + ", myProps=" + myProps + ", myListMap=" + myListMap + "]";
}
}
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?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">

<!-- 注册MyCollections -->
<bean id="myCollections" class="com.node.ba04.MyCollections">
<!-- 设值注入 -->
<!-- Array -->
<property name="myStrs">
<array>
<value>北京</value>
<value>上海</value>
</array>
</property>

<!-- List -->
<property name="myList">
<list>
<ref bean="myStudent" />
<ref bean="myStudent2" />
</list>
</property>

<!-- Map -->
<property name="myMap">
<map>
<!--
key:map的key的值
value:map的value
-->
<entry key="height" value="170"></entry>
<entry key="weight" value="65"></entry>
</map>
</property>

<!-- Set -->
<property name="mySet">
<set>
<value>海淀区</value>
<value>大兴区</value>
</set>
</property>

<!-- Properties -->
<property name="myProps">
<props>
<!--
key:key
value:开始标签和结束标签之间
-->
<prop key="tel">010-123456</prop>
<prop key="address">大兴</prop>
</props>
</property>

<!-- List<Map<String, String>> -->
<property name="myListMap">
<list>
<map>
<entry key="height" value="170cm"></entry>
<entry key="weight" value="65kg"></entry>
</map>

<map>
<entry key="city" value="石家庄"></entry>
<entry key="privince" value="河北"></entry>
</map>
</list>
</property>
</bean>

<bean id="myStudent" class="com.node.ba04.Student">
<constructor-arg name="myName" value="张三" />
<constructor-arg name="myAge" value="26" />
<constructor-arg name="mySchool" ref="mySchool" />
</bean>

<bean id="myStudent2" class="com.node.ba04.Student">
<constructor-arg index="0" value="张三" />
<constructor-arg index="1" value="28" />
<constructor-arg index="2" ref="mySchool" />
</bean>

<bean id="mySchool" class="com.node.ba04.School">
<property name="name" value="实验小学" />
<property name="address" value="海淀区" />
</bean>
</beans>
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
<?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">

<!-- 注册MyCollections -->
<bean id="myCollections" class="com.node.ba05.MyCollections">
<!-- 设值注入 -->
<!-- Array可以使用array,也可以使用list -->
<property name="myStrs">
<list>
<value>北京-array</value>
<value>上海-array</value>
</list>
</property>
<!-- Array也可以使用value。value使用,分隔的多个值 -->
<property name="myStrs" value="河北,山东">
</property>

<!-- List -->
<!-- 该List只有一个元素 -->
<property name="myList" ref="myStudent">

</property>

<!-- Set -->
<!-- 该Set只有一个元素 -->
<property name="mySet" value="海淀区-set,大兴区-set">

</property>
</beans>

4. 引用类型的自动注入

对于引用类型属性的注入,也可不在配置文件中显式的明确注入,可以通过为bean标签设置autowire属性值,为引用类型的属性进行隐式自动注入(默认不自动注入引用类型属性)。

可以分为两种:

  1. byName: 根据名称自动注入

  2. byType: 根据类型自动注入

4.1 byName

当配置文件中被调用者Bean的id值与代码中调用者Bean类的属性名相同时,可以使用byName方式,让容器自动将被调用者Bean注入给调用者Bean。容器是通过调用者的Bean类的属性名与配置文件的被调用者Bean的id进行比较而实现自动注入的。