配置文件 import.xml
 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <objects xmlns="http://www.springframework.net" xmlns:aop="http://www.springframework.net/aop">
 3 
 4   <!--带参数的构造函数-->
 5   <object id="HelloWorld1" type="IocApp.HelloWorld, IocApp">
 6     <constructor-arg name="name" value="Wang" />
 7     <constructor-arg name="age" value="23" />
 8   </object>
 9 
10   <!--不带参数的构造函数,并且给属性赋值-->
11   <object id="HelloWorld2" type="IocApp.HelloWorld, IocApp">
12     <property name="Name" value="Tom" />
13   </object>
14   <!--静态工厂方法创建对象-->
15   <object id="HelloWorld3" type="IocApp.HelloWorld, IocApp" factory-method="Create"/>
16 
17   <!--实例工厂方法创建对象-->
18   <object id="HelloWorld4" type="IocApp.HelloWorld, IocApp" factory-method="Build" factory-object="builder" />
19   <object id="builder" type="IocApp.Builder, IocApp" />
20 
21   <!--对象初始化方法-->
22   <object id="HelloWorld5" type="IocApp.HelloWorld, IocApp" init-method="Init" singleton="false"/>
23 
24   <!--工厂类 的静态工厂方法 不带参数-->
25   <object id="HelloWorld6" type="IocApp.Builder, IocApp" factory-method="StaticBuild" />
26 
27   <!--工厂类 的静态工厂方法 带参数  且参数为Type类型-->
28   <object id="HelloWorld7" type="IocApp.Builder, IocApp" factory-method="StaticBuild">
29      <constructor-arg name="type" value="IocApp.HelloWorld, IocApp"/>       
30   </object> 
31 </objects>
 1     using Spring.Core;
 2     using Spring.Context;
 3     using Spring.Context.Support;
 4 
 5     public class Builder
 6     {
 7         public  HelloWorld Build()
 8         {
 9             return new HelloWorld("Builder", 30);
10         }
11 
12         public static HelloWorld StaticBuild()
13         {
14             return new HelloWorld("StaticBuilder", 21);
15         }
16 
17 
18         public static  object StaticBuild(Type type)
19         {
20            return   Activator.CreateInstance(type);           
21         }
22 
23     }
24 
25     public class HelloWorld
26     {
27         private string name;
28         private int age;
29 
30         public HelloWorld(string name, int age)
31         {
32             this.name = name;
33             this.age = age;
34         }
35         public string Name
36         {
37             get
38             {
39                 return name;
40             }
41             set
42             {
43                 name=value;
44 
45             }
46         }
47       
48         public HelloWorld()
49         {
50            
51         }
52 
53         public void init()
54         {
55             name="init Name";
56             age=29;
57         }
58         public static HelloWorld Create()
59         {
60             return new HelloWorld("wangQiang", 25);
61         }
62         public override string ToString()
63         {
64             return String.Format("Name={0}; Age={1}", name, age);
65         }
66     }
67 
68     public class Program
69     {
70         static  IApplicationContext context = new XmlApplicationContext(@"..\..\import.xml");
71         static void Main(string[] args)
72         {
73 
74 
75             for (int i=1; i<=7; i++)
76                 Show("HelloWorld"+i);
77  
78             Console.ReadKey();
79         }
80 
81         static void Show(string id)
82         {
83             object o1=context.GetObject(id);
84             object o2=context.GetObject(id);
85             Console.WriteLine(o1);
86             Console.WriteLine(id+ "  IsEqueals:"+ object.ReferenceEquals(o1, o2)); // output: true
87         }
88        
89     }

 

posted on 2013-03-16 15:53  极简  阅读(262)  评论(0编辑  收藏  举报