程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Spring.Net學習筆記(3)-創建對象,spring.net學習筆記

Spring.Net學習筆記(3)-創建對象,spring.net學習筆記

編輯:C#入門知識

Spring.Net學習筆記(3)-創建對象,spring.net學習筆記


一、開發環境

編譯器:VS2013

.Net版本:.net framework4.5

二、涉及程序集

Spring.Core.dll:1.3

Common.Logging

三、開發過程

1.項目結構

namespace CreateObjects { public class Person { public override string ToString() { return "我是Person類"; } public class Heart { public override string ToString() { return "我是Heart類"; } } } }

3.添加GenericClass.cs

namespace CreateObjects
{
    public class GenericClass<T>
    {
    }
}

4.添加StaticObjectsFactory.cs

namespace CreateObjects
{
    public static class StaticObjectsFactory
    {
        public static Person CreateInstance()
        {
            return new Person();
        }
    }
}

5.添加InstanceObjectsFactory.cs

namespace CreateObjects
{
    public class InstanceObjectsFactory
    {
        public Person CreateInstance()
        {
            return new Person();
        }
    }
}

6.配置Spring.Net

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler,Spring.Core"/>
    </sectionGroup>
  </configSections>
  <spring>
    <context>
      <resource uri="config://spring/objects"></resource>
    </context>
    <objects>
      <!--構造器-->
      <object id="person" type="CreateObjects.Person,CreateObjects"></object>
      <!--嵌套類型-->
      <object id="heart" type="CreateObjects.Person+Heart,CreateObjects"></object>

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

      <!--靜態工廠-->
      <object id="staticObjectsFactory"
              type="CreateObjects.StaticObjectsFactory,CreateObjects"
             factory-method="CreateInstance" ></object>

      <!--實例工廠-->
      <!--工廠-->
      <object id="instanceObjectsFactory" type="CreateObjects.InstanceObjectsFactory,CreateObjects"></object>
      <!--創建的對象-->
      <object id="instancePerson" factory-method="CreateInstance" factory-object="instanceObjectsFactory"></object>

    </objects>
  </spring>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

7.控制台程序如下

namespace CreateObjects
{
    class Program
    {
        static void Main(string[] args)
        {
            IApplicationContext context = ContextRegistry.GetContext();
            Person person = context.GetObject("person") as Person;
            Console.WriteLine(person);

            Person.Heart heart = context.GetObject("heart") as Person.Heart;
            Console.WriteLine(heart);

            GenericClass<int> genericClass = context.GetObject("genericClass") as GenericClass<int>;
            Console.WriteLine(genericClass);

            Person personStaticFactory = context.GetObject("staticObjectsFactory") as Person;
            Console.WriteLine(personStaticFactory);

            Person personInstanceFactory = context.GetObject("instancePerson") as Person;
            Console.WriteLine(personInstanceFactory);
            Console.ReadKey();
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved