Spring.Net学习系列 - 第一篇HelloWorld

之所以写这篇文章是因为网上很多例子都是不可以用的,大多纯粹是误人子弟。

这篇文章纯粹是一个HelloWorld,希望能帮到大家。至于配置里需要注意的地方可参考其他文章,这里仅列出可用的项目代码及下载。

开发环境:VS2005(打了SP1的补丁)

步骤一:新建一个控制台项目

步骤二:引用Spring.Core.dll

步骤三:新建App.config代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="spring">
            <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>            
        </sectionGroup>
    </configSections>
    <spring>
        <context>
            <resource uri="file://spring.xml.config"/>
        </context>
    </spring>
</configuration>

步骤四:新建spring.xml.config(这个名字可自定,如需改名,则在App.config的rescource里也需一并修改),代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
    <object id="hello" type="SpringNetDemo.Hello">
        <property name="HelloWord" value="Hello!你能看到这个证明你成功了!"/>
    </object>
</objects>

步骤五:修改项目原来的Program.cs文件:

using System;
using System.Collections.Generic;
using System.Text;
using Spring.Context;
using Spring.Context.Support;

namespace SpringNetDemo
{
    public class Hello
    {
        private string helloword;

        public string HelloWord
        {
            get { return this.helloword; }
            set { this.helloword = value; }
        }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            IApplicationContext context = ContextRegistry.GetContext();
            Hello hello = (Hello)context.GetObject("hello");
            Console.Write(hello.HelloWord);
            Console.Read();
        }
    }
}

说明:这里的hello.HelloWord就是Spring.Net通过xml的配置实现注入的。有问题的可以留言。

点击这里下载所有代码

posted @ 2012-05-10 11:47  佳乐比海  阅读(2324)  评论(7编辑  收藏  举报