Spring.Net学习笔记-容器的使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring.Net学习笔记-容器的使用相关的知识,希望对你有一定的参考价值。
一、下载地址:
http://www.springframework.net/download.html
二、相关程序集
Spring.Net容器定义在程序集Spring.Core.dll中,它依赖于Common.Logging.dll。该程序集位于
Spring.NET-1.3.1\Spring.NET\bin\net\4.0\release目录下
三、创建容器
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); } }
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); } }
4.使用配置文件
需要使用一个新的配置处理器: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(); } }
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>
以上是关于Spring.Net学习笔记-容器的使用的主要内容,如果未能解决你的问题,请参考以下文章