本文接下来要阐述的是IOC对象怎么创建,主要有3种方法:
第一种:构造器创建(嵌套类型对象的创建需要用“+”号来连接被嵌套的类型。如果在PersonDao中嵌套了类型Person)
类库代码:
1 namespace Dao 2 { 3 public interface IPersonDao 4 { 5 void Save(); 6 } 7 public class PersonDao : IPersonDao 8 { 9 public void Save() 10 { 11 Console.WriteLine("保存 Person"); 12 } 13 } 14 15 public class PersonDao1 : IPersonDao 16 { 17 public void Save() 18 { 19 Console.WriteLine("保存 Person1"); 20 } 21 } 22 }
配置文件还是跟以前一样:
1 <objects xmlns="http://www.springframework.net"> 2 <!--这里放容器里面的所有节点--> 3 <description>An example that demonstrates simple IoC features.</description> 4 <!--name 必须要唯一的,type=类的全名称,所在的程序集--> 5 <object name="PersonDao" type="Dao.PersonDao1, Dao"> 6 </object> 7 </objects>
1 string[] xmlFiles = new string[] 2 { 3 "assembly:../../../../Objects.xml" 4 }; 5 IApplicationContext context = new XmlApplicationContext(xmlFiles); 6 IObjectFactory factory = (IObjectFactory)context; 7 Console.WriteLine(factory.GetObject("personDao").ToString());
不知道为什么路径是这样而不是../../这样的,望有高手看到能解决我心中疑问。
第二中:静态工厂即简单工厂创建对象
1 public class StaticObjectsFactory 2 { 3 public static IPersonDao CreateInstance() 4 { 5 return new PersonDao1(); 6 } 7 }
调用:
1 IApplicationContext context = new XmlApplicationContext(xmlFiles); 2 IObjectFactory factory = (IObjectFactory)context; 3 Console.WriteLine(factory.GetObject("staticObjectsFactory").ToString());
<object name="staticObjectsFactory" type="CreateObjects.StaticObjectsFactory, CreateObjects" factory-method="CreateInstance"/>
第三:使用实例工厂创建对象
使用实例工厂创建对象需要先定义一个工厂,然后设置factory-object和factory-method属性,且满足 实例工厂方法所在的对象必须也要配置在同一容器(或父容器)中 和 对象定义就不能包含type属性
类库代码:
1 public class StaticObjectsFactory 2 { 3 public IPersonDao CreateInstance() 4 { 5 return new instanceObjectsFactory().CreateInstance(); 6 } 7 } 8 9 public class instanceObjectsFactory 10 { 11 public IPersonDao CreateInstance() 12 { 13 return new PersonDao1(); 14 } 15 }
1 <objects xmlns="http://www.springframework.net"> 2 <!--这里放容器里面的所有节点--> 3 <description>An example that demonstrates simple IoC features.</description> 4 <object name="staticObjectsFactory" type="Dao.StaticObjectsFactory,Dao"/> 5 <object name="instancePersonDao" factory-method="CreateInstance" factory-object="staticObjectsFactory"/> 6 </objects> 7
调用:
1 string[] xmlFiles = new string[] 2 { 3 "assembly:../../../../Objects.xml" 4 }; 5 IApplicationContext context = new XmlApplicationContext(xmlFiles); 6 IObjectFactory factory = (IObjectFactory)context; 7 IPersonDao dao = (IPersonDao)factory.GetObject("instancePersonDao"); 8 if (dao != null) 9 { 10 dao.Save(); 11 Console.WriteLine("我是IoC方法"); 12 Console.ReadKey(); 13 }
第四、泛型类型的创建
1 public class Pig<T> 2 { 3 public void Save() 4 { 5 Console.WriteLine(1); 6 } 7 } 8 9 <object id="pig" type="Dao.Pig<int>, Dao"/> 10 11 string[] xmlFiles = new string[] 12 { 13 "assembly:../../../../Objects.xml" 14 }; 15 IApplicationContext context = new XmlApplicationContext(xmlFiles); 16 IObjectFactory factory = (IObjectFactory)context; 17 object obj = factory.GetObject("pig"); 18 19 Console.WriteLine(obj.ToString()); 20 Console.ReadLine();
泛型类型的创建比较类型于以上几种创建方式,可以有通过构造器创建,还可以通过静态或者实例工厂创建。但是设置type属性的时候要注意:左尖括号<要替换成字符串“<”,因为在XML中左尖括号会被认为是小于号。
时间: 2024-10-13 21:44:44