4.spring对象的创建(静态工厂 实例工厂 泛型,嵌套类型)

1.原料类

namespace CreateObjects
{
    public class GenericClass<T>
    {

}
}

PersonDao 类 包含嵌套类型

namespace CreateObjects
{
    public class PersonDao
    {
        public class Person
        {
            public override string ToString()
            {
                return "我是全套类Person";
            }
        }

public override string ToString()
        {
            return "我是PersonDao";
        }
    }
}
3.实例工厂类

namespace CreateObjects
{
    public class InstanceObjectsFactory
    {
        public PersonDao CreateInstance()
        {
            return new PersonDao();
        }
    }
}
4.静态工厂类

namespace CreateObjects
{
    public static class StaticObjectsFactory
    {
        public static PersonDao CreateInstance()
        {
            return new PersonDao();
        }
    }
}
5.最重要的部分xml 构造器 xml文件

<?xml version="1.0" encoding="utf-8" ?>

<objects xmlns="http://www.springframework.net"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.net
        http://www.springframework.net/xsd/spring-objects.xsd">

<!--构造器-->
  <object id="personDao" type="CreateObjects.PersonDao, CreateObjects" />

<!--嵌套类型-->
  <object id="person" type="CreateObjects.PersonDao+Person, CreateObjects" />

<!--静态工厂-->
  <object id="staticObjectsFactory" type="CreateObjects.StaticObjectsFactory, CreateObjects" factory-method="CreateInstance"/>

<!-- 实例工厂 -->
  <object id="instanceObjectsFactory" type="CreateObjects.InstanceObjectsFactory, CreateObjects" /><!--工厂-->
  <object id="instancePersonDao" factory-method="CreateInstance" factory-object="instanceObjectsFactory" /> <!--创建的对象-->

<!-- 泛型类型 GenericClass<int> -->
  <object id="genericClass" type="CreateObjects.GenericClass&lt;int>, CreateObjects" />

</objects>

6.调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Context;
using Spring.Objects.Factory;
using Spring.Context.Support;

namespace CreateObjects
{
    class Program
    {
        static void Main(string[] args)
        {
            CreateByConstructor();  // 构造器创建
            CreateNested();  // 嵌套类型创建
            CreateByStaticFactory();  // 静态工厂创建
            CreateByInstanceFactory();  // 实例工厂创建
            CreateGenericClass();  // 创建泛型

Console.ReadLine();
        }

/// <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());
        }

/// <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());
        }

/// <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());
        }

/// <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());
        }

/// <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());
        }
    }
}

上一篇:一劳永逸的搞定 flex 布局


下一篇:解读Flex布局及其基本使用