学过了前面的课程,我们来一起学习Spring.NET的IoC容器对象的创建。
创建对象一般有3种方式:1.构造器创建,2.静态工厂创建,3.实例工厂创建
多数情况下,容器会根据对象定义中的type属性值去直接调用相应类型的某个构造器。另外,容器也可以调用工厂方法来创建对象,这时type属性的值就应该是包含工厂方法的类型(按:而不是要创建的类型,但通过该对象定义的名称获取的则是由工厂方法所创建的对象)。工厂方法的产品对象可以是工厂方法所在的类型,也可以是其它类型(按:很多情况下工厂方法位于单独的类型中),这无关紧要。(摘自Spring.NET中文手册)
一、通过构造器创建对象
通过构造器创建对象需要满足这几个条件:1.指明对象类型type="类全名,程序集名"(<object id="personDao" type="CreateObjects.PersonDao, CreateObjects" />),也可以使用强命名type="System.Windows.Forms.Form, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089"。2.有一个无参的构造函数或者默认构造函数(依赖注入的时候需要外部可以调用的构造函数,如pubilc修饰的,构造函数可以带参数)。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
<!--
构造器
-->
<
object
id
="personDao"
type
="CreateObjects.PersonDao, CreateObjects"
/>
CreateByConstructor
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> /// <summary>
/// 构造器创建
/// </summary>
static void CreateByConstructor()
{
string[] xmlFiles = new string[]
{
"assembly://CreateObjects/CreateObjects/Objects.xml"
};
IApplicationContext context = new XmlApplicationContext(xmlFiles);
IObjectFactory factory = (IObjectFactory)context;
Console.WriteLine(factory.GetObject("personDao").ToString());
}
嵌套类型对象的创建需要用“+”号来连接被嵌套的类型。如果在PersonDao中嵌套了类型Person
public class PersonDao
{
public override string ToString()
{
return "我是PersonDao";
}
class Person {
public override string ToString()
{
return "我是Person";
}
}
}
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
<!--
嵌套类型
-->
<
object
id
="person"
type
="CreateObjects.PersonDao+Person, CreateObjects"
/>
CreateNested
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> /**//// <summary>
/// 嵌套类型创建
/// </summary>
static void CreateNested()
{
string[] xmlFiles = new string[]
{
"assembly://CreateObjects/CreateObjects/Objects.xml"
};
IApplicationContext context = new XmlApplicationContext(xmlFiles);
IObjectFactory factory = (IObjectFactory)context;
Console.WriteLine(factory.GetObject("person").ToString());
}
二、静态工厂创建
public class PersonDao
{
public override string ToString()
{
return "我是PersonDao";
}
class Person {
public override string ToString()
{
return "我是Person";
}
}
//该方法必须是public的
public static PersonDao createInstance()
{
return new PersonDao();
}
}
使用静态工厂创建对象需要配置factory-method属性
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
<!--
静态工厂
-->
<
object
id
="staticObjectsFactory"
type
="CreateObjects.StaticObjectsFactory, CreateObjects"
factory-method
="CreateInstance"
/>
CreateByStaticFactory
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> /**//// <summary>
/// 静态工厂创建
/// </summary>
static void CreateByStaticFactory()
{
string[] xmlFiles = new string[]
{
"assembly://CreateObjects/CreateObjects/Objects.xml"
};
IApplicationContext context = new XmlApplicationContext(xmlFiles);
IObjectFactory factory = (IObjectFactory)context;
Console.WriteLine(factory.GetObject("staticObjectsFactory").ToString());
}
三、使用实例工厂创建对象
class InstanceObjectsFactory {
public PersonDao create()
{
return new PersonDao();
}
}
使用实例工厂创建对象需要先定义一个工厂,然后设置factory-object和factory-method属性,且满足 实例工厂方法所在的对象必须也要配置在同一容器(或父容器)中 和 对象定义就不能包含type属性
instanceObjectsFactory
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> <!-- 实例工厂 -->
<object id="instanceObjectsFactory" type="CreateObjects.InstanceObjectsFactory, CreateObjects" /><!--工厂-->
<object id="instancePersonDao" factory-method="CreateInstance" factory-object="instanceObjectsFactory" /> <!--创建的对象-->
CreateByInstanceFactory
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> /**//// <summary>
/// 实例工厂创建
/// </summary>
static void CreateByInstanceFactory()
{
string[] xmlFiles = new string[]
{
"assembly://CreateObjects/CreateObjects/Objects.xml"
};
IApplicationContext context = new XmlApplicationContext(xmlFiles);
IObjectFactory factory = (IObjectFactory)context;
Console.WriteLine(factory.GetObject("instancePersonDao").ToString());
}
四、泛型类型的创建
泛型类型的创建比较类型于以上几种创建方式,可以有通过构造器创建,还可以通过静态或者实例工厂创建。但是设置type属性的时候要注意:左尖括号<要替换成字符串“<”,因为在XML中左尖括号会被认为是小于号。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
<!--
泛型类型 GenericClass<int>
-->
<
object
id
="genericClass"
type
="CreateObjects.GenericClass<int>, CreateObjects"
/>
CreateGenericClass
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> /**//// <summary>
/// 创建泛型
/// </summary>
static void CreateGenericClass()
{
string[] xmlFiles = new string[]
{
"assembly://CreateObjects/CreateObjects/Objects.xml"
};
IApplicationContext context = new XmlApplicationContext(xmlFiles);
IObjectFactory factory = (IObjectFactory)context;
object obj = factory.GetObject("genericClass");
Console.WriteLine(obj.ToString());
}
以上就是对象创建的几种方式。