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的配置实现注入的。有问题的可以留言。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?