实例工厂方法创建对象


using System;
using System.Collections;
using System.Text;
using System.Threading;
using Spring.Core;
using Spring.Context;
using Spring.Context.Support;
using System.Configuration;
namespace ConsoleApplication1.SpringNet

 /// <summary>
 /// 实例工厂方法创建对象

 /// </summary>
 public class Creator
 {
  public HelloWorld Create()
  {
   return new HelloWorld();
  }
 }

 public class HelloWorld
 {
  internal HelloWorld()
  {
  }
  public override string ToString()
  {
   return "实例工厂方法创建对象";
  }
 }

 public class Program
 {
  static void Main()
  {
   IApplicationContext ctx = ConfigurationSettings.GetConfig("spring/context") as IApplicationContext;
             object o = ctx.GetObject("HelloWorld");
   object o2 = ctx.GetObject("HelloWorld");
   Console.WriteLine(o);
   Console.WriteLine(object.ReferenceEquals(o, o2)); // output: true
   Console.ReadLine();
  }
 }
}

<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" />
  </context>
  <objects xmlns="http://www.springframework.net">
   <object id="HelloWorld" type="ConsoleApplication1.SpringNet.HelloWorld, test2" factory-method="Create" factory-object="Creator">  
           
            </object>
           <object id="Creator" type="ConsoleApplication1.SpringNet.Creator,test2"/>

  </objects>
 </spring>
</configuration>

posted @ 2007-06-27 15:39  每天积累一点  阅读(382)  评论(0编辑  收藏  举报