IoC相关组件功能介绍
Spring.Core:该程序集是Spring.Net IoC容器的基础.
IObjectFactory:该接口提供了一个高级的配置机制并能管理任何类型的对象.
IApplicationContext:IObjectFactory的子接口,更容易的集成Spring.NET的AOP特性,消息资源处理(用于国际化),事件传播,应用程序特定层上下文,例如用于web应用程序中的WebApplicationContext
配置元数据:
先看看XML元数据的基本结构
<objects xmlns="http://www.springframework.net"> <object id="..." type="..."> <!-- collaborators and configuration for this object go here --> </object> <object id="...." type="..."> <!-- collaborators and configuration for this object go here --> </object> <!-- more object definitions go here --> </objects>
id属性是一个字符串,你用来识别个体对象定义,type属性定义对象的类型和使用完全限定类型名称,包括程序集名称
举例说明如何使用IApplicationContext
IApplicationContext context = new XmlApplicationContext("services.xml", "data-access.xml");
构造函数中,第一个参数为服务层对象配置文件,第二个为数据访问层配置对象
services.xml
View Code
<objects xmlns="http://www.springframework.net"> <object id="PetStore" type="PetStore.Services.PetStoreService, PetStore"> <property name="AccountDao" ref="AccountDao"/> <property name="ItemDao" ref="ItemDao"/> <!-- additional collaborators and configuration for this object go here --> </object> <!-- more object definitions for services go here --> </objects>
daos.xml
View Code
<objects xmlns="http://www.springframework.net"> <object id="AccountDao" type="Petstore.Dao.HibernateAccountDao, PetStore"> <!-- additional collaborators and configuration for this object go here --> </object> <object id="ItemDao" type="Petstore.Dao.HibernateItemDao, PetStore"> <!-- additional collaborators and configuration for this object go here --> </object> <!-- more object definitions for data access objects go here --> </objects>
从上面的两个文件中可以看出,服务层包含PetStoreService类,和基于NHibernate对象/关系映射框架的两个数据访问对象类型HibernateAccountDao和HibernateItemDao.属性名元素指的是类的属性名称,ref元素指的是给对象属性定义的别名.id和ref元素之间的依赖关系,详见元对象的依赖注入配置.
从非默认的资源位置加载配置元数据:
上面加载配置文件的方式是假设配置文件位于bin\Debug根目录之下,你还可以使用IResource接口从其它路径加载资源文件,如:
IResource input = new FileSystemResource(@"D:\Objects.xml"); //实际物理路径 IObjectFactory factory = new XmlObjectFactory(input);
另一种方法是在程序集下找配置文件
IApplicationContext context = new XmlApplicationContext( "file:///services.xml", "assembly://MyAssembly/MyDataAccess/data-access.xml");
IObjectFactory factory = (IObjectFactory) context;
使用如下的格式访问程序集内嵌的资源文件,assembly://<AssemblyName>/<NameSpace>/<ResourceName>.
在App.config/Web.config中声明配置容器
View Code
<spring> <context> <resource uri="file://services.xml"/> <resource uri="assembly://MyAssembly/MyDataAccess/data-access.xml"/> </context> </spring>
上面的文件省略了Content元素的type属性,在一个独立的程序中type属性默认为Spring.Context.Support.XmlApplicationContext类,而在Web程序中默认为WebApplicationContext.例如:
View Code
<spring> <context type="Spring.Context.Support.XmlApplicationContext, Spring.Core"> <resource uri="file:///services.xml"/> <resource uri="assembly://MyAssembly/MyDataAccess/data-access.xml"/> </context> </spring>
从App.config/Web.config自定义配置部分得到IApplicationContext的引用,可以使用如下代码
IApplicationContext ctx = ContextRegistry.GetContext();
这个ContextRegistry用于实例化应用程序上下文和执行服务定位器的方式访问其他对象。
View Code
<?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://objects.xml"/> </context> </spring> </configuration>
你还可以使用IApplicationContext构造器从多个xml文件中加载定义的对象
View Code
<objects xmlns="http://www.springframework.net"> <import resource="services.xml"/> <import resource="resources/messageSource.xml"/> <import resource="/resources/themeSource.xml"/> <object id="object1" type="..."/> <object id="object2" type="..."/> </objects>
