1
2 1------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
2
di的注入上次讲了一些,这次主要阐述域属性的自动注入
先讲byType方式
看名字就知道是根据类型进行自动注入
案例:
实体类:(俩个,一个学生类,一个汽车类)
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 1package cn.dawn.day06autowire;
2
3
4/**
5 * Created by Dawn on 2018/3/5.
6 */
7//student类
8public class Student {
9 private String name;
10 private Integer age;
11 private Car car;
12
13 //带参构造
14 public Student(String name, Integer age, Car car) {
15 this.name = name;
16 this.age = age;
17 this.car = car;
18 }
19
20 //无参构造
21 public Student() {
22 }
23
24 public String getName() {
25 return name;
26 }
27
28 public void setName(String name) {
29 this.name = name;
30 }
31
32 public Integer getAge() {
33 return age;
34 }
35
36 public void setAge(Integer age) {
37 this.age = age;
38 }
39
40 public Car getCar() {
41 return car;
42 }
43
44 public void setCar(Car car) {
45 this.car = car;
46 }
47}
48
49
50
51
52package cn.dawn.day06autowire;
53
54/**
55 * Created by Dawn on 2018/3/5.
56 */
57public class Car {
58 private String color;
59 private String type;
60
61 public String getColor() {
62 return color;
63 }
64
65 public void setColor(String color) {
66 this.color = color;
67 }
68
69 public String getType() {
70 return type;
71 }
72
73 public void setType(String type) {
74 this.type = type;
75 }
76}
77
在配置文件中:
1
2
3
4
5
6
7
8
9
10
11
12 1 <!--汽车的bean-->
2 <bean id="car" class="cn.dawn.day06autowire.Car">
3 <property name="color" value="黑色"></property>
4 <property name="type" value="奥迪"></property>
5 </bean>
6
7 <!--装配student-->
8 <bean id="student" class="cn.dawn.day06autowire.Student" autowire="byType">
9 <property name="name" value="马云"></property>
10 <property name="age" value="18"></property>
11 </bean>
12
单测方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 1package cn.dawn.day06autowire;
2
3import org.junit.Test;
4import org.springframework.context.ApplicationContext;
5import org.springframework.context.support.ClassPathXmlApplicationContext;
6
7/**
8 * Created by Dawn on 2018/3/3.
9 */
10public class test20180306 {
11
12
13 @Test
14 /*di自动注入*/
15 public void t01(){
16 ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day06autowire.xml");
17 Student student = (Student) context.getBean("student");
18 System.out.println("学生"+student.getName()+"开着"+student.getCar().getColor()+"的"+student.getCar().getType());
19 }
20}
21
单测后可以正常执行
但是问题来了:如果有一个类继承Car,并且在spring的配置文件中配置了他的bean,那么byType还可以吗?
结果是不行的,它报错的解释是,不能自动装配,有比一个多的car类型,所以,引出了另外一种方式byName
————————————————————————————————————-
byName的方式,要求实体类的关联的那个对象名要和上面配置的bean的id一样,就可以自动装配,我的上面汽车的bean的id是car,说明只要Student类中的关联的Car类型的对象的名字是car就可以自动装配
代码
1
2
3
4
5 1 <bean id="student" class="cn.dawn.day06autowire.Student" autowire="byName">
2 <property name="name" value="马云"></property>
3 <property name="age" value="18"></property>
4 </bean>
5
结果依旧,正常运行