參見spring中文幫助.chm文檔
代碼參考whyspringofioc
1.首先來個Bean1類
1
package com.zyl.spring;
2
3
import java.util.Date;
4
import java.util.List;
5
import java.util.Map;
6
import java.util.Set;
7
public class Bean1 {
8
private String strValue;
9
private int intValue;
10
private List listValue;
11
private Set setValue;
12
private String[] arrayValue; //數組
13
private Map mapValue;
14
private Date dateValue;
15
public Date getDateValue() {
16
return dateValue;
17
}
18
public void setDateValue(Date dateValue) {
19
this.dateValue = dateValue;
20
}
21
public String getStrValue() {
22
return strValue;
23
}
24
public void setStrValue(String strValue) {
25
this.strValue = strValue;
26
}
27
public int getIntValue() {
28
return intValue;
29
}
30
public void setIntValue(int intValue) {
31
this.intValue = intValue;
32
}
33
public List getListValue() {
34
return listValue;
35
}
36
public void setListValue(List listValue) {
37
this.listValue = listValue;
38
}
39
public Set getSetValue() {
40
return setValue;
41
}
42
public void setSetValue(Set setValue) {
43
this.setValue = setValue;
44
}
45
public String[] getArrayValue() {
46
return arrayValue;
47
}
48
public void setArrayValue(String[] arrayValue) {
49
this.arrayValue = arrayValue;
50
}
51
public Map getMapValue() {
52
return mapValue;
53
}
54
public void setMapValue(Map mapValue) {
55
this.mapValue = mapValue;
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

我們可以看到Bean1類中有很多各種各樣的屬性.相應的創建setter/getter
2.然后相應配置xml:
1
<?xml version="1.0" encoding="UTF-8"?>
2
<beans
3
xmlns="http://www.springframework.org/schema/beans"
4
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
6
<!-- id名稱是隨便取的 -->
7
<bean id="bean11" class="com.zyl.spring.Bean1">
8
<!-- name的值是類中定義的變量名 -->
9
<property name="strValue" value="依賴注入的值"/>
10
<property name="intValue" value="12311"/>
11
<property name="listValue">
12
<list>
13
<value>list1</value>
14
<value>list2</value>
15
<value>list3</value>
16
</list>
17
</property>
18
<property name="setValue">
19
<set>
20
<value>set1</value>
21
<value>set2</value>
22
</set>
23
</property>
24
<property name="arrayValue">
25
<list>
26
<value>array1</value>
27
<value>array2</value>
28
</list>
29
</property>
30
<property name="mapValue">
31
<map>
32
<entry key="key1" value="value1"/>
33
<entry key="key2" value="value2"/>
34
</map>
35
</property>
36
<property name="dateValue">
37
<value>2008/03/06</value>
38
</property>
39
</bean>
注意,屬性名(property name)是Bean1類中的變量名哦.
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

通過value屬性給屬性賦值,也可以用ref來指定引用.
這樣就可以了~
3.我們寫一個junit來測試




























注意:BeanFactory factory =new ClassPathXmlApplicationContext("gogogo-*.xml") 這樣的寫法,可以把xml分開至多個xml中.spring會找到前綴為gogogo-的xml.(bean id不可以重復哦!)
通過這樣的方式就可以打印出效果了.
可是時間處理卻會報錯!
4.我們需要個時間編輯器:UtilDateEdit.java
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

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

format的格式可以在xml中配置,讓spring來注入吧.
5.下面看xml中時間編輯器的配置:
1
<bean id="UtilDateEdit666666666" class="org.springframework.beans.factory.config.CustomEditorConfigurer"><!-- 這個class是api中 -->
2
<!-- 將屬性編輯器注入"<property name="customEditors">"這里面的customEditors是固定的 -->
3
<property name="customEditors"> <!-- 這個name是固定哦 -->
4
<map>
5
<entry key="java.util.Date">
6
<!-- 內部使用的類 -->
7
<bean class="com.zyl.spring.UtilDateEdit">
8
<property name="format" value="yyyy/MM/dd"/>
9
</bean>
10
</entry>
11
</map>
12
</property>
13
</bean>
這樣就大功告成了.
2

3

4

5

6

7

8

9

10

11

12

13

打印效果:.(傳說中見證奇跡的時刻?)
sdfs2008/03/06
strValue依賴注入的值
intValue12311
listValue[list1, list2, list3]
setValue[set1, set2]
strArray[Ljava.lang.String;@4e280c
mapValue{key1=value1, key2=value2}
dateValueThu Mar 06 00:00:00 CST 2008
華麗的分割線-=-=-=-=-=-=-=-=-=-
sdfs2008/03/06
當有時間的處理發生時,spring會在配置文件中找到并調用時間編輯器.至于為什么.....恩..參考下spring的代碼吧.
這次就到這里吧 --end--;