指定类型构造方法以外的初始化方法
指定类型构造方法以外的初始化方法
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
{
public class HelloWorld
{
/// <summary>
/// 对象初始化方法我们可以在配置文件中使用 "init-method" 指定类型构造方法以外的初始化方法。
/// 该方法会在对象构造方法之后被容器调用执行
/// </summary>
public HelloWorld()
{
//Console.WriteLine("构造函数");
}
public void Init()
{
Console.WriteLine("Init...");
}
}
public class Program
{
static void Main()
{
IApplicationContext ctx = ConfigurationSettings.GetConfig("spring/context") as IApplicationContext;
object o = ctx.GetObject("HelloWorld");
Console.WriteLine(o);
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, test3" init-method="Init" >
</object>
</objects>
</spring>
</configuration>