Spring-依赖注入
sjyvv 2020-01-05 18:38:34
spring
- 构造器注入
- set方式注入
- 依赖注入:Set注入
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器来注入
- 拓展方式注入
- p命名空间
- c命名空间
# set方式注入
POJO类
Student.java
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> bobbies;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
//setter和getter省略。。
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
测试类
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("ApplicationContext.xml");
System.out.println(applicationContext.getBean(Student.class));
/*
Student{
name='蔡徐坤',
address=Address{address='北京'},
books=[book1, book2, book3],
bobbies=[唱, 跳, rap],
card={a=a1, b=b1, c=c1},
games=[lol, wow, 123],
wife='null',
info={password=pass, username=admin}}
*/
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
xml配置
<bean id="address" class="com.Address">
<property name="address" value="北京"/>
</bean>
<bean id="student" class="com.Student">
<property name="name" value="蔡徐坤"/>
<!--bean注入-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books">
<array>
<value>book1</value>
<value>book2</value>
<value>book3</value>
</array>
</property>
<!--list注入-->
<property name="bobbies">
<list>
<value>唱</value>
<value>跳</value>
<value>rap</value>
</list>
</property>
<!--map注入-->
<property name="card">
<map>
<entry key="a" value="a1"/>
<entry key="b" value="b1"/>
<entry key="c" value="c1"/>
</map>
</property>
<!--set注入-->
<property name="games">
<set>
<value>lol</value>
<value>wow</value>
<value>123</value>
</set>
</property>
<!--null注入-->
<property name="wife">
<null/>
</property>
<!--list注入-->
<property name="info">
<props>
<prop key="username">admin</prop>
<prop key="password">pass</prop>
</props>
</property>
</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
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
# 拓展方式注入
# p命名空间
必须导入
xmlns:p="http://www.springframework.org/schema/p"
1
直接注入属性值
<bean name="classic" class="com.example.ExampleBean">
<property name="email" value="someone@somewhere.com"/>
</bean>
<bean name="p-namespace" class="com.example.ExampleBean"
p:email="someone@somewhere.com"/>
1
2
3
4
5
6
2
3
4
5
6
# c命名空间
必须导入
xmlns:c="http://www.springframework.org/schema/c"
1
直接构造器注入
<!-- traditional declaration with optional argument names -->
<bean id="beanOne" class="x.y.ThingOne">
<constructor-arg name="thingTwo" ref="beanTwo"/>
<constructor-arg name="thingThree" ref="beanThree"/>
<constructor-arg name="email" value="something@somewhere.com"/>
</bean>
<!-- c-namespace declaration with argument names -->
<bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
c:thingThree-ref="beanThree" c:email="something@somewhere.com"/>
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10