一、下载地址:
http://www.springframework.net/download.html
二、相关程序集
Spring.Net容器定义在程序集Spring.Core.dll中,它依赖于Common.Logging.dll。该程序集位于
Spring.NET-1.3.1Spring.NETin et4.0 elease目录下
三、创建容器
1.编程方式的容器
使用Spring.Context.Support.StaticApplicationContxt直接创建容器
public class Person { public string Name { get; set; } public override string ToString() { return "This is Person"; } }
class Program { static void Main(string[] args) { //创建容器 Spring.Context.Support.StaticApplicationContext context = new StaticApplicationContext(); //注册Person类 context.RegisterPrototype("Person", typeof(Person), null); //从容器中获取Person对象 Person person = context.GetObject("Person") as Person; Console.WriteLine(person); } }
2.使用xml方式(注意xml的输出方式)
处理xml的配置处理器:Spring.Context.Support.XmlApplicationContext
<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net"> <object id="Person" type="CPrj.Person,Cprj"></object> </objects>
class Program { static void Main(string[] args) { Spring.Context.Support.XmlApplicationContext context = new XmlApplicationContext("objects.xml"); Person person = context.GetObject("Person") as Person; Console.WriteLine(person); } }
注:<object>的父节点必须是<objects>,且objects的xmlns元素是必须的
3.使用配置文件+xml文件
处理器Spring.Context.Support.ContextHandler在启动程序时候加载配置信息
<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net"> <object id="Person" type="CPrj.Person,Cprj"></object> </objects>
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"></section> </sectionGroup> </configSections> <spring> <context> <resource uri="file://objects.xml"></resource> </context> </spring> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
class Program { static void Main(string[] args) { Spring.Context.IApplicationContext context = Spring.Context.Support.ContextRegistry.GetContext(); Person person = context.GetObject("Person") as Person; Console.WriteLine(person); } }
注:context节点的默认type就是 Spring.Context.Support.XmlApplicationContext,Spring.Core 。故可省去
<spring> <context type="Spring.Context.Support.XmlApplicationContext,Spring.Core"> <resource uri="file://objects.xml"></resource> </context> </spring>
4.使用配置文件
xml对象定义也可以放在DotNet的标准应用程序配置文件中,此时必须为<objects>节点预先注册相应的节点处理器
需要使用一个新的配置处理器:Spring.Context.Support.DefaultSectionHandler
它可以帮助我们解析spring配置信息
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"></section> <section name="objects" type="Spring.Context.Support.DefaultSectionHandler,Spring.Core"/> </sectionGroup> </configSections> <spring> <context> <resource uri="config://spring/objects"></resource> </context> <objects xmlns="http://www.springframework.net"> <object id="Person" type="CPrj.Person,CPrj"></object> </objects> </spring> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
class Program { static void Main(string[] args) { Spring.Context.IApplicationContext context = Spring.Context.Support.ContextRegistry.GetContext(); Person person = context.GetObject("Person") as Person; Console.WriteLine(person); Console.ReadKey(); } }
注:spring和context节点名称不是任意的,必须是spring和context。Spring.Net本身将“spring/context”作为字符串常亮定义在AbstractApplicationContext类中以表示上下文的节点名称
5.混合使用外部配置文件和嵌入的配置
<?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="file://objects.xml"/> <resource uri="config://spring/objects"/> </context> <objects> <object id="Person" type="CPrj.Person,CPrj"></object> </objects> </spring> </configuration>
四、参考文章
http://www.cnblogs.com/haogj/archive/2011/06/10/2077540.html