S书W经
netgod's blog
我的首页
文章
相册
圈子
留言
管理
 
    当前所在页面:首页>>文章>>Spring.NET框架起航之IOC入门篇-教你写HelloWorld
Spring.NET框架起航之IOC入门篇-教你写HelloWorld
    作者:netgod 来源: 发表时间:2007-12-18

 
 
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容器的配置和基本使用方法
 
 

(阅读 )   评论数(:2)
评论】 【收藏】
评论:共2条
spring.net是哪个组织写的东西?
评论人: erikchang     评论时间: 2007-12-18 19:57:09
Spring.NET是Spring组织的一个子项目,整个Spring.NET开发团队由Spring的部分开发人员和Hibernate(NHibernate)的部分开发人员共同组成的.
评论人: netgod     评论时间: 2007-12-19 09:22:09

发表评论:
发表人:
评论: 
    
 
关于我们 | 诚聘英才 | 联系我们 | 广告业务 | 网站地图 | 法律声明

EasyJF开源团队版权所有  建议使用1024*768分辨率