參見spring中文幫助.chm文檔
代碼參考whyspringofioc
1.首先來個(gè)Bean1類

 1package com.zyl.spring;
 2
 3import java.util.Date;
 4import java.util.List;
 5import java.util.Map;
 6import java.util.Set;
 7public class Bean1 {
 8   private String strValue;
 9   private int intValue;
10   private List listValue;
11   private Set setValue;
12   private String[] arrayValue; //數(shù)組
13   private Map mapValue;
14   private Date dateValue;
15public Date getDateValue() {
16    return dateValue;
17}

18public void setDateValue(Date dateValue) {
19    this.dateValue = dateValue;
20}

21public String getStrValue() {
22    return strValue;
23}

24public void setStrValue(String strValue) {
25    this.strValue = strValue;
26}

27public int getIntValue() {
28    return intValue;
29}

30public void setIntValue(int intValue) {
31    this.intValue = intValue;
32}

33public List getListValue() {
34    return listValue;
35}

36public void setListValue(List listValue) {
37    this.listValue = listValue;
38}

39public Set getSetValue() {
40    return setValue;
41}

42public void setSetValue(Set setValue) {
43    this.setValue = setValue;
44}

45public String[] getArrayValue() {
46    return arrayValue;
47}

48public void setArrayValue(String[] arrayValue) {
49    this.arrayValue = arrayValue;
50}

51public Map getMapValue() {
52    return mapValue;
53}

54public void setMapValue(Map mapValue) {
55    this.mapValue = mapValue;
56}

57   
58}

59

我們可以看到Bean1類中有很多各種各樣的屬性.相應(yīng)的創(chuàng)建setter/getter
2.然后相應(yīng)配置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類中的變量名哦.
通過value屬性給屬性賦值,也可以用ref來指定引用.
這樣就可以了~
3.我們寫一個(gè)junit來測(cè)試

package test;

import junit.framework.TestCase;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zyl.spring.Bean1;
import com.zyl.spring.Bean2;


public class test extends TestCase {
    
//
    public void testInjection1(){
        BeanFactory factory 
=new ClassPathXmlApplicationContext("gogogo-*.xml");//加上配置文件xml的名字
       
        Bean1 bean1
=(Bean1)factory.getBean("bean11");//bean11為xml中取的id名字
        
        System.out.println(
"strValue"+ bean1.getStrValue());
        System.out.println(
"intValue"+ bean1.getIntValue());
        System.out.println(
"listValue"+ bean1.getListValue());
        System.out.println(
"setValue"+ bean1.getSetValue());
        System.out.println(
"strArray"+ bean1.getArrayValue());
        System.out.println(
"mapValue"+ bean1.getMapValue());
        System.out.println(
"dateValue"+ bean1.getDateValue());
        System.out.println(
"華麗的分割線-=-=-=-=-=-=-=-=-=-");
        
    }

注意:BeanFactory factory =new ClassPathXmlApplicationContext("gogogo-*.xml") 這樣的寫法,可以把xml分開至多個(gè)xml中.spring會(huì)找到前綴為gogogo-的xml.(bean id不可以重復(fù)哦!)
通過這樣的方式就可以打印出效果了.
可是時(shí)間處理卻會(huì)報(bào)錯(cuò)!
4.我們需要個(gè)時(shí)間編輯器:UtilDateEdit.java
 1package com.zyl.spring;
 2
 3import java.beans.PropertyEditorSupport;
 4import java.text.SimpleDateFormat;
 5import java.util.Date;
 6
 7public class UtilDateEdit extends PropertyEditorSupport {
 8    //轉(zhuǎn)換時(shí)間的功能
 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 轉(zhuǎn)為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

format的格式可以在xml中配置,讓spring來注入吧.
5.下面看xml中時(shí)間編輯器的配置:
 1<bean id="UtilDateEdit666666666" class="org.springframework.beans.factory.config.CustomEditorConfigurer"><!-- 這個(gè)class是api中 -->
 2<!-- 將屬性編輯器注入"<property name="customEditors">"這里面的customEditors是固定的 -->
 3    <property name="customEditors"> <!-- 這個(gè)name是固定哦 -->
 4         <map>
 5             <entry key="java.util.Date"> 
 6               <!-- 內(nèi)部使用的類 -->
 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>
這樣就大功告成了.
打印效果:.(傳說中見證奇跡的時(shí)刻?)
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
當(dāng)有時(shí)間的處理發(fā)生時(shí),spring會(huì)在配置文件中找到并調(diào)用時(shí)間編輯器.至于為什么.....恩..參考下spring的代碼吧.
這次就到這里吧  --end--;