Lost !

-----hard working for the furture.

导航

统计

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 on   失落''80  阅读(125)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示