14.Bean属性的注入方式

 

Bean属性的注入方式

Bean的集合属性的注入

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.course.vo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Department {

private List<String> titleList;
private List<User> staffList;

private Set<String> titleSet;
private Set<User> staffSet;

private Map<String, String> databaseMap;
private Map<Integer, User> staffMap;

private String[] titleArray;
private User[] staffArray;

private Properties properties;

public List<String> getTitleList() {
return titleList;
}

public void setTitleList(List<String> titleList) {
this.titleList = titleList;
}

public List<User> getStaffList() {
return staffList;
}

public void setStaffList(List<User> staffList) {
this.staffList = staffList;
}

public Set<String> getTitleSet() {
return titleSet;
}

public void setTitleSet(Set<String> titleSet) {
this.titleSet = titleSet;
}

public Set<User> getStaffSet() {
return staffSet;
}

public void setStaffSet(Set<User> staffSet) {
this.staffSet = staffSet;
}

public Map<String, String> getDatabaseMap() {
return databaseMap;
}

public void setDatabaseMap(Map<String, String> databaseMap) {
this.databaseMap = databaseMap;
}

public Map<Integer, User> getStaffMap() {
return staffMap;
}

public void setStaffMap(Map<Integer, User> staffMap) {
this.staffMap = staffMap;
}

public String[] getTitleArray() {
return titleArray;
}

public void setTitleArray(String[] titleArray) {
this.titleArray = titleArray;
}

public User[] getStaffArray() {
return staffArray;
}

public void setStaffArray(User[] staffArray) {
this.staffArray = staffArray;
}

public Properties getProperties() {
return properties;
}

public void setProperties(Properties properties) {
this.properties = properties;
}

@Override
public String toString() {
return "Department [titleList=" + titleList + ", staffList=" + staffList + ", titleSet=" + titleSet
+ ", staffSet=" + staffSet + ", databaseMap=" + databaseMap + ", staffMap=" + staffMap + ", titleArray="
+ Arrays.toString(titleArray) + ", staffArray=" + Arrays.toString(staffArray) + ", properties="
+ properties + "]";
}
}
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?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="title04" class="java.lang.String">
<constructor-arg name="original" value="title04"></constructor-arg>
</bean>
<bean id="staff02" class="com.course.vo.User">
<property name="id" value="2"></property>
<property name="name" value="staff02"></property>
<property name="address" value="address02"></property>
</bean>

<!-- list集合属性注入 -->
<bean id="departmentList" class="com.course.vo.Department">
<property name="titleList">
<list>
<value>title01</value>
<value>title02</value>
<value>title03</value>
<ref bean="title04" />
<bean class="java.lang.String">
<constructor-arg name="original" value="title05"></constructor-arg>
</bean>
</list>
</property>
<property name="staffList">
<list>
<bean class="com.course.vo.User">
<property name="id" value="1"></property>
<property name="name" value="staff01"></property>
<property name="address" value="address01"></property>
</bean>
<ref bean="staff02" />
</list>
</property>
</bean>

<!-- Set属性的注入 -->
<bean id="departmentSet" class="com.course.vo.Department">
<property name="titleSet">
<set>
<value>title01</value>
<value>title02</value>
<value>title03</value>
<ref bean="title04" />
<bean class="java.lang.String">
<constructor-arg name="original" value="title05"></constructor-arg>
</bean>
</set>
</property>
<property name="staffSet">
<set>
<bean class="com.course.vo.User">
<property name="id" value="1"></property>
<property name="name" value="staff01"></property>
<property name="address" value="address01"></property>
</bean>
<ref bean="staff02" />
</set>
</property>
</bean>

<!-- Map属性的注入 -->
<bean id="urlVal" class="java.lang.String">
<constructor-arg name="original" value="http://ip:port/database"></constructor-arg>
</bean>
<bean id="usernameKey" class="java.lang.String">
<constructor-arg name="original" value="username"></constructor-arg>
</bean>
<bean id="passwordKey" class="java.lang.String">
<constructor-arg name="original" value="root"></constructor-arg>
</bean>
<bean id="passwordVal" class="java.lang.String">
<constructor-arg name="original" value="123456"></constructor-arg>
</bean>

<bean id="departmentMap" class="com.course.vo.Department">
<property name="databaseMap">
<map>
<entry key="driver" value="com.mysql.jdbc.Driver"></entry>
<entry key="url" value-ref="urlVal"></entry>
<entry key-ref="usernameKey" value="root"></entry>
<entry key-ref="passwordKey" value-ref="passwordVal"></entry>
</map>
</property>
<property name="staffMap">
<map>
<entry key="1">
<bean class="com.course.vo.User">
<property name="id" value="1"></property>
<property name="name" value="staff01"></property>
<property name="address" value="address01"></property>
</bean>
</entry>
<entry key="2" value-ref="staff02"></entry>
</map>
</property>
</bean>

<!-- 数组属性的注入 -->
<bean id="departmentArray" class="com.course.vo.Department">
<property name="titleArray">
<array>
<value>title01</value>
<value>title02</value>
<value>title03</value>
<ref bean="title04" />
<bean class="java.lang.String">
<constructor-arg name="original" value="title05"></constructor-arg>
</bean>
</array>
</property>
<property name="staffArray">
<array>
<bean class="com.course.vo.User">
<property name="id" value="1"></property>
<property name="name" value="staff01"></property>
<property name="address" value="address01"></property>
</bean>
<ref bean="staff02" />
</array>
</property>
</bean>

<!-- Properties属性的注入 -->
<bean id="departmentProperties" class="com.course.vo.Department">
<property name="properties">
<props>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="url">http://ip:port/database</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</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
33
34
35
36
37
package com.test;

import java.util.Date;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.course.vo.Department;
import com.course.vo.User;

public class TestMain {

public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

Department departmentList = (Department) applicationContext.getBean("departmentList");
System.out.println(departmentList);
// Department [titleList=[title01, title02, title03, title04, title05], staffList=[User [id=1, name=staff01, address=address01, hireDate=null], User [id=2, name=staff02, address=address02, hireDate=null]], titleSet=null, staffSet=null, databaseMap=null, staffMap=null, titleArray=null, staffArray=null, properties=null]

Department departmentSet = (Department) applicationContext.getBean("departmentSet");
System.out.println(departmentSet);
// Department [titleList=null, staffList=null, titleSet=[title01, title02, title03, title04, title05], staffSet=[User [id=1, name=staff01, address=address01, hireDate=null], User [id=2, name=staff02, address=address02, hireDate=null]], databaseMap=null, staffMap=null, titleArray=null, staffArray=null, properties=null]

Department departmentMap = (Department) applicationContext.getBean("departmentMap");
System.out.println(departmentMap);
// Department [titleList=null, staffList=null, titleSet=null, staffSet=null, databaseMap={driver=com.mysql.jdbc.Driver, url=http://ip:port/database, username=root, root=123456}, staffMap={1=User [id=1, name=staff01, address=address01, hireDate=null], 2=User [id=2, name=staff02, address=address02, hireDate=null]}, titleArray=null, staffArray=null, properties=null]

Department departmentArray = (Department) applicationContext.getBean("departmentArray");
System.out.println(departmentArray);
// Department [titleList=null, staffList=null, titleSet=null, staffSet=null, databaseMap=null, staffMap=null, titleArray=[title01, title02, title03, title04, title05], staffArray=[User [id=1, name=staff01, address=address01, hireDate=null], User [id=2, name=staff02, address=address02, hireDate=null]], properties=null]

Department departmentProperties = (Department) applicationContext.getBean("departmentProperties");
System.out.println(departmentProperties);
// Department [titleList=null, staffList=null, titleSet=null, staffSet=null, databaseMap=null, staffMap=null, titleArray=null, staffArray=null, properties={password=123456, driver=com.mysql.jdbc.Driver, url=http://ip:port/database, username=root}]

}
}

xml自动注入

UserService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.course.service.impl;

import com.course.dao.UserDao;
import com.course.service.UserService;
import com.course.vo.User;

public class UserServiceImpl implements UserService {

private UserDao userDao;

// set()
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

@Override
public User getUserInfo(Integer id) {
User user = userDao.getUser(id);
return user;
}
}

UserController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.course.controller;

import com.course.service.UserService;
import com.course.vo.User;

public class UserController {

private UserService userService;

// set()
public void setUserService(UserService userService) {
this.userService = userService;
}

public User getUserById(Integer id) {
User user = userService.getUserInfo(id);
return user;
}
}

application.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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">


<!--
autowire: 自动注入
- byName: 根据类的属性名,将IoC容器中id=属性名的对象注入进来
- byType: 根据类的属性类型,将IoC容器该类型的对象或该类型的子类对象注入进来
-->
<bean id="userDao" class="com.course.dao.impl.UserDaoImpl">
</bean>

<bean id="userService" class="com.course.service.impl.UserServiceImpl" autowire="byName">
</bean>

<bean id="userController" class="com.course.controller.UserController" autowire="byType">
</bean>
</beans>

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.course.controller.UserController;
import com.course.vo.User;

public class TestMain {

public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
System.out.println(applicationContext);

UserController userController = (UserController) applicationContext.getBean("userController");
User user = userController.getUserById(1);
System.out.println(user);
// User [id=1, name=Tom1, address=location1]
}
}