HelloWorld,虽然简单,却是每个程序员的必经之路,这里给大家介绍Spring.NET框架的安装及使用Spring.NET编写一个最简单的HelloWorld程序来初步了解IOC,并做为进一步学习的基础。
一、Spring.NET框架的获取及安装
在http://sourceforge.net/project/showfiles.php?group_id=106751 上大家找到Spring.NET下载Spring.NET-1.1.exe ,然后安装用VISUAL STUDIO2005编译,这里只使用Spring.Core.dll. 1.1
二、使用Spring.NET框架编写最简单的HelloWorld程序
使用VisualStudio建立一个名为HelloWorld的项目,然后添加对Spring.Core.dll的引用,编写代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Spring.Core;
using Spring.Context;
using Spring.Context.Support;
namespace SpringNet.HelloWorld
{
public class HelloWorld
{
public override string ToString()
{
return "HelloWorld!";
}
}
public class Program
{
static void Main(string[] args)
{
IApplicationContext context = new XmlApplicationContext(@"springapplication.xml");
object o = context.GetObject("HelloWorld");
Console.WriteLine(o);
}
}
}
然后在项目中建立一个springapplication.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="HelloWorld" type=" SpringNet.HelloWorld">
</object>
</objects>
运行该程序,如果出现HelloWorld表示你的程序运行成功。
以上程序使用到了Spring.Net IOC容器中的IApplicationContext核心接口来进行对象的注入,在下一篇文章中我们将介绍Spring.Net IOC容器的配置和基本使用方法