spring 的依赖注入(DI)

来源:互联网 发布:量子纠缠知乎 编辑:程序博客网 时间:2024/05/21 01:29

Spring DI

DI(依赖注入)就是给属性赋值。

spring容器的执行顺序

考虑依赖注入之后的Spring的执行顺序如下
spring容器的执行顺序spring容器的执行顺序

依赖注入的方法

setter注入

使用类的setter方法进行注入是最常用的一种方法,即通过调用类的setXXX()方法,注入所依赖的属性。
举例说明,写person类和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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class Person {

public Person(){
System.out.println("person");
}

private Long pid;
private String name;
private Student student;
private List list;
private Set sets;
private Properties properties;
private Map map;

public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSets() {
return sets;
}
public void setSets(Set sets) {
this.sets = sets;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}

}

1
2
3
4
5
6
7
8
public class Student {
public Student(){
System.out.println("student");
}
public void student(){
System.out.println("student");
}
}

在spring的配置文件中把person和student放入到spring容器中

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
<bean id="student" class="cn.zju.spring.Student"></bean>
<bean id="person" class="cn.zju.spring.Person">
<!--
property代表该bean的属性
pid,name看做是基本类型,用value赋值
student是一个引用类型,用ref赋值
-->

<property name="pid" value="10"></property>
<property name="name" value="Tom"></property>
<property name="student" ref="student"></property>
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
<ref bean="student"/>
</list>
</property>
<property name="sets">
<set>
<value>set1</value>
<value>set2</value>
<ref bean="student"/>
</set>
</property>
<property name="map">
<map>
<entry key="m1">
<value>map1</value>
</entry>
<entry key="m2">
<ref bean="student"/>
</entry>
</map>
</property>
<property name="properties">
<props>
<prop key="p1">
prop1
</prop>
<prop key="p2">
prop2
</prop>
</props>
</property>
</bean>

在上述配置信息中,property的name属性值必须与setter方法名对应,即property的name值是对应的setXXX方法名的set后的单词,且首字母变小写。

构造方法注入

除了使用setter方法注入外,还可以使用构造方法注入,即通过调用带参数的构造方法注入所依赖的属性。
举例,假设Person类中有如下的构造方法:

1
2
3
4
5
public Person(Long pid,String name,Student student){
this.pid = pid;
this.name = name;
this.student = student;
}

那么在配置文件中可以通过如下配置,使用上述构造方法对Person实例注入属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
<bean id="student" class="cn.zju.spring.Student"></bean>
<bean id="person" class="cn.zju.spring.Person">
<!--
指定的构造函数中的某一个参数
index 参数的索引值,从0开始
type 参数的类型
value 如果参数是基本类型,则用value赋值
ref 如果参数是引用类型,则用ref赋值
-->

<constructor-arg index="0" type="java.lang.Long" value="10"></constructor-arg>
<constructor-arg index="1" type="java.lang.String" value="Tom "></constructor-arg>
<constructor-arg index="2" ref="student"></constructor-arg>
</bean>

在平时开发中,主要使用上述两种依赖注入的方法。





original link:http://longliqiang88.github.io/2015/08/15/Spring%E5%AD%A6%E4%B9%A02%EF%BC%9ASpring%20DI%EF%BC%88%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%EF%BC%89/

0 0
原创粉丝点击