我们在Bean1中的int的setter方法中添加一个打印:
改成这样:
1public void setIntValue(int intValue) {
2 System.out.println("一旦注入就会打印.我是bean1的setInt方法");
3 this.intValue = intValue;
4}
2 System.out.println("一旦注入就会打印.我是bean1的setInt方法");
3 this.intValue = intValue;
4}
执行testInjection1我们会发现结果是:
1sdfs2008/03/06
2一旦注入就会打印.我是bean1的setInt方法
3strValue依赖注入的值
4intValue12311
5listValue[list1, list2, list3]
6setValue[set1, set2]
7strArray[Ljava.lang.String;@4e280c
8mapValue{key1=value1, key2=value2}
9dateValueThu Mar 06 00:00:00 CST 2008
10华丽的分割线-=-=-=-=-=-=-=-=-=-
为什么会这样呢.2一旦注入就会打印.我是bean1的setInt方法
3strValue依赖注入的值
4intValue12311
5listValue[list1, list2, list3]
6setValue[set1, set2]
7strArray[Ljava.lang.String;@4e280c
8mapValue{key1=value1, key2=value2}
9dateValueThu Mar 06 00:00:00 CST 2008
10华丽的分割线-=-=-=-=-=-=-=-=-=-
我们看到第一行打印的是 sdfs2008/03/06
可以知道,1.注入的时候最先执行的是编辑器.
编辑器代码如下:
1package com.zyl.spring;
2
3import java.beans.PropertyEditorSupport;
4import java.text.SimpleDateFormat;
5import java.util.Date;
6
7public 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
3import java.beans.PropertyEditorSupport;
4import java.text.SimpleDateFormat;
5import java.util.Date;
6
7public 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
1package test;
2
3import junit.framework.TestCase;
4
5import org.springframework.beans.factory.BeanFactory;
6import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8import com.zyl.spring.Bean1;
9import com.zyl.spring.Bean2;
10
11
12public 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
3import junit.framework.TestCase;
4
5import org.springframework.beans.factory.BeanFactory;
6import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8import com.zyl.spring.Bean1;
9import com.zyl.spring.Bean2;
10
11
12public 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
3.而这之后,就是在xml中的属性值注入.以此我们可以更加理解spring的注入过程.