我们在Bean1中的int的setter方法中添加一个打印:
改成这样:
1
public void setIntValue(int intValue) {
2
System.out.println("一旦注入就会打印.我是bean1的setInt方法");
3
this.intValue = intValue;
4
}

2

3

4

执行testInjection1我们会发现结果是:
1
sdfs2008/03/06
2
一旦注入就会打印.我是bean1的setInt方法
3
strValue依赖注入的值
4
intValue12311
5
listValue[list1, list2, list3]
6
setValue[set1, set2]
7
strArray[Ljava.lang.String;@4e280c
8
mapValue{key1=value1, key2=value2}
9
dateValueThu Mar 06 00:00:00 CST 2008
10
华丽的分割线-=-=-=-=-=-=-=-=-=-
为什么会这样呢.
2

3

4

5

6

7

8

9

10

我们看到第一行打印的是 sdfs2008/03/06
可以知道,1.注入的时候最先执行的是编辑器.
编辑器代码如下:
1
package com.zyl.spring;
2
3
import java.beans.PropertyEditorSupport;
4
import java.text.SimpleDateFormat;
5
import java.util.Date;
6
7
public class UtilDateEdit extends PropertyEditorSupport {
8
//转换时间的功能
9
private String format;
10
public String getFormat() {
11
return format;
12
}
13
public void setFormat(String format) {
14
this.format = format;
15
}
16
//private String format="yyyy-MM-dd" ;
17
public void setAsText(String text) throws IllegalArgumentException {
18
//将传入的string 转为java.util.date
19
System.out.println("sdfs"+text);
20
SimpleDateFormat sdf= new SimpleDateFormat(format);
21
22
try {
23
Date date =sdf.parse(text);
24
this.setValue(date);
25
} catch (Exception e) {
26
// TODO: handle exception
27
}
28
29
30
}
31
32
33
}
34
testInjection1的代码如下:
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

1
package test;
2
3
import junit.framework.TestCase;
4
5
import org.springframework.beans.factory.BeanFactory;
6
import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8
import com.zyl.spring.Bean1;
9
import com.zyl.spring.Bean2;
10
11
12
public class test extends TestCase {
13
//
14
public void testInjection1(){
15
BeanFactory factory =new ClassPathXmlApplicationContext("gogogo-*.xml");//加上配置文件xml的名字
16
17
Bean1 bean1=(Bean1)factory.getBean("bean11");//bean11为xml中取的id名字
18
19
System.out.println("strValue"+ bean1.getStrValue());
20
System.out.println("intValue"+ bean1.getIntValue());
21
System.out.println("listValue"+ bean1.getListValue());
22
System.out.println("setValue"+ bean1.getSetValue());
23
System.out.println("strArray"+ bean1.getArrayValue());
24
System.out.println("mapValue"+ bean1.getMapValue());
25
System.out.println("dateValue"+ bean1.getDateValue());
26
System.out.println("华丽的分割线-=-=-=-=-=-=-=-=-=-");
27
28
}
29
/*
30
public void testInjection2(){
31
BeanFactory factory =new ClassPathXmlApplicationContext("gogogo-*.xml");//加上配置文件xml的名字
32
33
Bean2 bean2=(Bean2)factory.getBean("bean2");//读取xml中id为bean2的东东
34
35
System.out.println("bean2.bean3.id="+bean2.getBean3().getId());
36
System.out.println("bean2.bean3.name="+bean2.getBean3().getName());
37
System.out.println("bean2.bean3.password="+bean2.getBean3().getPassword());
38
System.out.println("bean2.bean4.id="+bean2.getBean4().getId());
39
System.out.println("bean2.bean4.name="+bean2.getBean4().getName());
40
System.out.println("bean2.bean5.age="+bean2.getBean5().getAge());
41
}
42
43
*/
44
}
45
2 .而在运行编辑器之后,运行的是Bean1中setter方法.
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

3.而这之后,就是在xml中的属性值注入.以此我们可以更加理解spring的注入过程.