6.DI依赖注入
6.1构造器注入
上一章已经说过了。。。。
6.2Set方式注入【重点】
【环境搭建】
复杂类型
1
2
3
4
5
6
7
8
9
10
11public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
真实测试对象
1
2
3
4
5
6
7
8
9public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;1
1
Beans.xml
1
2
3
4
5
6
7
8
9
10
11
12
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--第一种普通值注入,value-->
<bean name="student" class="com.lwj.pojo.Student">
<property name="name" value="张三"></property>
</bean>
</beans>测试类
1
2
3
4
5
6.junit.Test
public void test1(){
ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml");
Student student= (Student)context.getBean("student");
System.out.println("student = " + student.getName());
}完善注入信息
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<bean id="address" class="com.lwj.pojo.Address">
<property name="address" value="山东"></property>
</bean>
<bean name="student" class="com.lwj.pojo.Student">
<!--第一种普通值注入,value-->
<property name="name" value="张三"></property>
<!--第二种注入,引用类型-->
<property name="address" ref="address"></property>
<!--第三种 数组-->
<property name="books">
<array>
<value>水浒</value>
<value>钢铁是怎样炼成的</value>
<value>三国演义</value>
</array>
</property>
<!--第四种 list-->
<property name="hobbys" >
<list>
<value>唱歌</value>
<value>跳舞</value>
</list>
</property>
<!--第四种 map-->
<property name="card">
<map>
<entry key="身份证" value="1212121212"></entry>
<entry key="银行卡" value="22344555555"></entry>
</map>
</property>
<!--第五种 set-->
<property name="games">
<set>
<value>csgo</value>
<value>lol</value>
</set>
</property>
<!--第六种 null-->
<property name="wife">
<null></null>
</property>
<!--第七种 properties-->
<property name="info" >
<props>
<prop key="学号">001</prop>
<prop key="成绩">87</prop>
</props>
</property>
</bean>完整结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18.junit.Test
public void test1(){
ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml");
Student student= (Student)context.getBean("student");
System.out.println("student = " + student);
/*
* student = Student{
* name='张三',
* address=com.lwj.pojo.Address@28eaa59a,
* books=[水浒, 钢铁是怎样炼成的, 三国演义],
* hobbys=[唱歌, 跳舞],
* card={身份证=1212121212, 银行卡=22344555555},
* games=[csgo, lol],
* wife='null',
* info={学号=001, 成绩=87}}
*
* */
}
6.3Bean的作用域
单例模式(Spring默认机制)
1
2
3<bean id="address" class="com.lwj.pojo.Address" scope="singleton">
<property name="address" value="山东"></property>
</bean>原型模式
每次从容器中GET的时候都会产生一个新对象
1
2
3<bean id="address" class="com.lwj.pojo.Address" scope="prototype">
<property name="address" value="山东"></property>
</bean>其余的request,session,application这些只能在web开发中遇到!