Spring.NET学习笔记2——环境搭建(基础篇)
我们先来一起学习一下怎样使用Spring.NET。
一、环境下载及安装
到Spring的官方网站下载Spring.NET框架的安装文件(Spring.NET-1.3.0-RC1.exe)。目前Spring.NET最新的版本是1.3。下载并解压后就可以了。
我们使用Spring.NET框架经常用到的一下几个文件:
Common.Logging.dll(必要)
Spring.Core.dll(必要)
Spring.Data.dll
Spring.Aop.dll(可选)
Spring.Data.NHibernate21.dll
Spring.Web.dll
在以后的博客里我们会学习一些与NHibernate和Asp.NET MVC结合的例子,可以到Hibernate的官方网站和Asp.NET的官方网站下载各自的框架安装文件。
在基于XML的工厂中,这些对象定义表现为一个或多个<object>子节点,它们的父节点必须是<objects>(按:objects节点的xmlns元素是必需的,必须根据不同的应用添加不同的命名空间,以便有IDE的智能提示(见Spring.NET中文手册)。
Object.XML
- <objects xmlns="http://www.springframework.net"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.net
- [url]http://www.springframework.net/xsd/spring-objects.xsd[/url]">
- <object id="" type="">
- </object>
- <object id="." type="">
- </object>
- </objects>
同样也可以找到Spring.NET解压目录下的Spring.NET-1.3.0-RC1\doc\schema,把里面的几个.xsd复制到VS2008安装目录下的Microsoft Visual Studio 9.0\Xml\Schemas文件夹。
必要的时候可以安装建立Spring.NET程序的模板Spring.NET-1.3.0-RC1\dev-support\vs.net-2008\install-templates.bat
二、建立一个Spring.NET应用程序
我们新建一个Objects.xml的文件,然后从Spring.NET手册中复制来一段配置模板
Objects.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
- [url]http://www.springframework.net/xsd/spring-objects.xsd[/url]">
- <object id="PersonDao" type="FirstSpringNetApp.PersonDao, FirstSpringNetApp">
- </object>
- </objects>
目前我找到了实例化Spring.NET容量的两种方式:
1.实际物理路径
- IResource input = new FileSystemResource(@"D:\Objects.xml"); //实际物理路径
- IObjectFactory factory = new XmlObjectFactory(input);
2.程序集下寻找配置文件
- string[] xmlFiles = new string[]
- {
- "file://Objects.xml"
- };
- IApplicationContext context = new XmlApplicationContext(xmlFiles);
- IObjectFactory factory = (IObjectFactory)context;
- Console.ReadLine();
目前我一般采用后者(程序集下寻找配置文件),这样维护起来比较方便。
这种方式需满足URI语法。
file://文件名
assembly://程序集名/命名空名/文件名
然而更好的方式是在配置文件App.config或Web.config添加自定义配置节点
在配置文件中要引入<objects xmlns="http://www.springframework.net"/>命名空间,否则程序将会无法实例化Spring.NET容器。
App.config
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <configSections>
- <sectionGroup name="spring">
- <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
- <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
- </sectionGroup>
- </configSections>
- <spring>
- <context>
- <resource uri="assembly://FirstSpringNetApp/FirstSpringNetApp/Objects.xml"/>
- <resource uri="config://spring/objects" />
- </context>
- <objects xmlns="http://www.springframework.net"/> <!--必要-->
- </spring>
- </configuration>
- IApplicationContext ctx = ContextRegistry.GetContext();
- Console.WriteLine(ctx.GetObject("PersonDao").ToString());
好了,Spring.NET的环境配置就先讲到这里。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2008-03-26 c# 二分查找
2008-03-26 几种字符串反转方法效率比较
2008-03-26 C#反转字符串效率最高的方法