现在的位置: 首页 > 综合 > 正文

SPRING.NET 1.3.2 学习4 –实例化对象

2013年10月09日 ⁄ 综合 ⁄ 共 2766字 ⁄ 字号 评论关闭

在Spring.NET下实例化对象有几种方法

1.使用构造器

<object id="exampleObject" type="Examples.ExampleObject, ExamplesLibrary"/>

 Examples.ExampleObject为编译成ExamplesLibrary程序集中的类,ExampleObject定义在Examples命名空间中

如果你用定义一个嵌套类,可以使用一个+号,例如Examples.ExampleObject类里定义了一个嵌套类Person,那么XML应像如下定义

<object id="exampleObject" type="Examples.ExampleObject+Person, ExamplesLibrary"/>

2.使用静态工厂方法

下面的XML是使用Examples.ExampleObjectFactory类中的静态方法CreateInstance来实例化对象

<object id="exampleObject" type="Examples.ExampleObjectFactory, ExamplesLibrary" factory-method="CreateInstance"/>

3.使用工厂方法实例创建对象

下面是使用一个工厂方法实例(使用依赖注入来配置这个类)来实例化对象

复制代码
<!-- the factory object, which contains an instance method called 'CreateInstance' -->
<object id="exampleFactory" type="...">
  <!-- inject any dependencies required by this object -->
</object>
<!-- the object that is to be created by the factory object -->
<object id="exampleObject"
      factory-method="CreateInstance"
      factory-object="exampleFactory"/>
复制代码

可见这个方法为方法2的升级版,可以配置工厂对象

4.泛型类型的对象创建

假如有一个这样的类

复制代码
namespace GenericsPlay
{
    public class FilterableList<T>
    {
        private List<T> list;
        
        private String name;
        public List<T> Contents
        {
            get { return list; }
            set { list = value; }
        }
        public String Name
        {
            get { return name; }
            set { name = value; }
        }
        
        public List<T> ApplyFilter(string filterExpression)
        {
            /// should really apply filter to list ;) 
            return new List<T>();
        }
    }
}
复制代码

xml配置应该像这样

<object id="myFilteredIntList" type="GenericsPlay.FilterableList&lt;int>, GenericsPlay">
  <property name="Name" value="My Integer List"/>
</object>

上面的";int"表示实例化的时候泛型为int,Name属性赋了一个初值

 5.使用静态工厂方法创建泛型类型对象

下面是一个例子:

需要创建的类

复制代码
 1 public class TestGenericObject<T, U>
 2 {
 3     public TestGenericObject()
 4     {
 5     }
 6     private IList<T> someGenericList = new List<T>();
 7     private IDictionary<string, U> someStringKeyedDictionary =
 8         new Dictionary<string, U>();
 9     public IList<T> SomeGenericList
10     {
11         get { return someGenericList; }
12         set { someGenericList = value; }
13     }
14     public IDictionary<string, U> SomeStringKeyedDictionary
15     {
16         get { return someStringKeyedDictionary; }
17         set { someStringKeyedDictionary = value; }
18     }
19 }
复制代码

工厂类

public class TestGenericObjectFactory
{
    public static TestGenericObject<V, W> StaticCreateInstance<V, W>()
    {
        return new TestGenericObject<V, W>();
    }
    public TestGenericObject<V, W> CreateInstance<V, W>()
    {
        return new TestGenericObject<V, W>();
    }
}

下面使用XML配置文件把两个类关联起来

<object id="myTestGenericObject"
        type="GenericsPlay.TestGenericObjectFactory, GenericsPlay"
        factory-method="StaticCreateInstance&lt;System.Collections.Generic.List&lt;int>,int>"
/>

上面的type为工厂类,factroy-method则为名为StaticCreateInstance的方法,System.Collections.Generic.List&lt;int表示第一个泛型类型为List<int>,第二个泛型依然为int

再看下面这个XML

<object id="exampleFactory" type="GenericsPlay.TestGenericObject&lt;int,string>, GenericsPlay"/>
<object id="anotherTestGenericObject"
        factory-object="exampleFactory" 
        factory-method="CreateInstance&lt;System.Collections.Generic.List&lt;int>,int>"/>

同上一个XML文件类似,唯一不同的是,使用了非静态方法创建对象,创建的实例为TestGenericObject<List<int>,int>

抱歉!评论已关闭.