进行构造函数的参数配置
进行构造函数的参数配置,构造函数参数由配置文件指定
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 HelloWorld
{
private string name;
private int age;
public HelloWorld(string name, int age)
{
this.name = name;
this.age = age;
}
public override string ToString()
{
return String.Format("Name={0}; Age={1}", name, age);
}
}
public class Program
{
static void Main(string[] args)
{
try
{
IApplicationContext ctx = ConfigurationSettings.GetConfig("spring/context") as IApplicationContext;
object o = ctx.GetObject("HelloWorld");
// object o1=ctx.GetObject("HelloWorld");
// Console.WriteLine(object.ReferenceEquals(o,o1));
Console.WriteLine(o);
Console.ReadLine();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
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, test" singleton="false">
-->
<object id="HelloWorld" type="ConsoleApplication1.SpringNet.HelloWorld, test" >
<constructor-arg name="name" value="Tom" />
<constructor-arg name="age" value="234" />
</object>
</objects>
</spring>
</configuration>