Spring.Net-DI依赖注入和Ioc控制反转
Spring.Core作为整个Spring框架的基础,实现了依赖注入的功能。Spring框架的其它模块都要依赖或扩展该模块。
IObjectFactory接口,该接口实现了工厂模式,使用它可以帮我们创建对象,与具体的程序逻辑解耦。
IApplicationContext是IObjectFactory的扩展,增加了许多企业级的功能,包括使用资源文件进行文本本地化,事件传播,资源装载等等。
Spring.Net的配置
使用Spring.Net创建对象,需要在程序配置文件中配置
1:配置spring.Core的核心处理类型
在configSections节点中新增Spring的sectionGroup节点,申明Spring.Net处理的类型
<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>
2:配置Spring框架如何创建对象的spring节点
spring定义了Spring.Net框架如何创建对象,Spring.Net框架使用这些配置信息,创建对象的。
每一个object节点定义了一个类型创建的配置信息,type属性中第一个参数是类型的完整名称,第二个参数是类型所在的dll名称
可以把spring节点中的objects节点单独放入另外一个Xml中
<spring> <context> <!--<resource uri="assembly://SpringNetStudy.Control/SpringNetStudy.Control/Objects.xml" />--> <resource uri="config://spring/objects" /> </context> <objects xmlns="http://www.springframework.net"> <!-- Objects节点可以移动到其它地方--> <description>一个简单的控制反转例子</description> <object id="PersonDao" type="SpringNetStudy.Control.PersonDao, SpringNetStudy.Control" /> </objects> </spring>
Spring.Net创建对象
1 IApplicationContext ctx = ContextRegistry.GetContext(); 2 IObjectFactory objectFactory = (IApplicationContext)ctx; 3 4 //IPersonDao personDao = ctx.GetObject("PersonDao") as IPersonDao; 5 IPersonDao personDao = objectFactory.GetObject("PersonDao") as IPersonDao; 6 personDao.Save();
创建IApplicationContext接口,使用该接口的GetObject()方法,其参数就是配置文件中的object节点的id属性值,依据type属性值,创建对象。返回object类型
IApplicationContext是IObjectFactory的扩展,可以将IApplicationContext转换成IObjectFactory接口,调用其GetObject()方法。
1 IResource input = new FileSystemResource(@"B:\Project\SpringNetStudy\SpringNetStudy\SpringNetStudy.Control\Objects.xml"); 2 IObjectFactory factory = new XmlObjectFactory(input); 3 IPersonDao personDao = factory.GetObject("PersonDao") as IPersonDao; 4 personDao.Save(); 5 Console.ReadLine();
在Sping.Net的配置文件中,将objects节点单独移出,放在一个单独的xml文件中,使用物理路径实例化IObjectFactory接口对象
1 string[] xmlFiles = new string[] { "file://Objects.xml" }; 2 IApplicationContext context = new XmlApplicationContext(xmlFiles); 3 IObjectFactory factory = (IObjectFactory)context; 4 IPersonDao personDao = factory.GetObject("PersonDao") as IPersonDao; 5 personDao.Save(); 6 Console.ReadLine();
也可以在代码中指定在程序集中下寻找配置文件,这就需要将Objects.xml文件属性复制到输出目录选为始终复制
而最常用的做法是,在配置文件App.config或者Web.Config中添加自定义配置节点,这种做法是和将Objects节点放在配置文件中,是一样的效果
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 4 <configSections> 5 <sectionGroup name="spring"> 6 <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" /> 7 <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> 8 </sectionGroup> 9 </configSections> 10 11 <spring> 12 13 <context> 14 <resource uri="file://Objects.xml"></resource> 15 <resource uri="config://spring/objects" /> 16 </context> 17 18 19 <objects xmlns="http://www.springframework.net"> 20 </objects> 21 22 </spring> 23 24 </configuration>
那么在程序中不需要指定Objects.xml的位置,直接创建IApplicationContext或者IObjectFactory接口对象
1 IApplicationContext ctx = ContextRegistry.GetContext(); 2 IObjectFactory objectFactory = (IObjectFactory)ctx; 3 4 //IPersonDao personDao = ctx.GetObject("PersonDao") as IPersonDao; 5 IPersonDao personDao = objectFactory.GetObject("PersonDao") as IPersonDao; 6 personDao.Save();