『Spring.Net』IoC 容器
2012-02-01 10:50
288 查看
什么是IoC
![](http://images.cnblogs.com/cnblogs_com/sitemanager/201202/201202011049491788.png)
如何使用Spring.NET的IoC功能
翻译一段文档中的原话做为IoC使用的开始,由于水平所限,是否完全正确,就不要过于深究的……
The Spring.Core assembly is the basis for Spring.NET's IoC container. The IObjectFactory interface provides an advanced configuration mechanism capable of managing any type of object. IApplicationContext is a sub-interface of IObjectFactory. It adds easier integration with Spring.NET's Aspect Oriented Programming (AOP) features, message resource handling (for use in internationalization), event propagation and application layer-specific context such as WebApplicationContext for use in web applications.
Spring.Core提供的基础的Spring.NET IoC 容器的功能。IObjectFactory接口提供一种能够管理任何类型对象的高级的配置机制。IApplicationContext是IObjectFactory的一个子接口。它增加便于整合Spring.NET 的AOP特性、信息资源处理(用于国际化),事件的传播和应用于应用程序,就像WebApplicationContext用于网络应用程序。
这段话中告诉我们两点最有需要知道的:要引用 Spring.Core
要在代码中使用 IObjectFactory 的某个子项
使用步骤:
引用 Spring.Core
建立配置文件(可以写在App.config中)
将配置文件读入程序
使用GetObject方法完成实例化过程
使用实例
<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 : 这里我把它看成是类的映射名称。因为在IoC中主要是使用反射的机制来实现的,所以这里可以看成是那个提供给反射的字符串。
type : 这里有两项,第一项是真正用于注入的那个「类」,第二项是程序集名称。
IApplicationContext context = new XmlApplicationContext("services.xml", "data-access.xml");
括号中可以是一个配置文件,也可以是多个。个数的多少主要看实际的需要。
配置文件的路径要是Debug文件夹中的。
// retrieve configured instance PetStoreService service = (PetStoreService) context.GetObject("PetStoreService");
// use configured instance IList userList = service.GetUserNames();
参考代码
<objects xmlns="http://www.springframework.net"> <!-- <import resource="services.xml"/> --> <object id="AccVipDao" type="newpos.dao.impl.emax.TAccVipDA, newpos.dao.impl.emax"> <!-- collaborators and configuration for this object go here --> </object> <!-- more object definitions go here --> </objects>
namespace newpos.test { class Program { static void Main(string[] args) { IApplicationContext context = new XmlApplicationContext("Objects.xml"); IAccVipDao vip = (IAccVipDao)context.GetObject("AccVipDao"); TAccVip vipInfo = vip.GetDataAllByVipNo("123456"); Console.WriteLine(vipInfo.EnterDate); Console.ReadLine(); } } }
其他
Property | More info |
type | Section 5.2.5, “Instantiating objects” |
id and name | Section 5.2.4.1, “Naming objects” |
singleton or prototype | Section 5.4, “Object Scopes” |
object properties | Section 5.3.1, “Dependency injection” |
constructor arguments | Section 5.3.1, “Dependency injection” |
autowiring mode | Section 5.3.6, “Autowiring collaborators” |
dependency checking mode | Section 5.3.7, “Checking for dependencies” |
initialization method | Section 5.6.1, “Lifecycle interfaces” |
destruction method | Section 5.6.1, “Lifecycle interfaces” |
相关文章推荐
- Spring.net(二)----初探IOC容器
- ASP.NET WebForm使用IoC容器(Spring.NET容器、Castle等等)
- 学习Spring.NET - IoC对象管理容器(1)
- 使用Spring.NET的IoC容器
- Spring.Net学习笔记四(IOC容器对象的作用域)
- 使用Spring.NET的IoC容器
- Spring.net(二)----初探IOC容器
- Castle IOC容器与Spring.NET配置之比较
- SPRING.NET 1.3.2 学习3--IOC容器
- Castle IOC容器与Spring.NET配置之比较
- Castle IOC容器与Spring.NET配置之比较
- 使Spring.NET的IOC容器支持动态加载的程序集
- IoC容器 - Spring.NET
- 使Spring.NET的IOC容器支持动态加载的程序集
- Spring.Net IoC容器+Observer模式
- Castle IOC容器与Spring.NET配置之比较
- 『Spring.NET』IoC 容器中对象的作用域
- Castle IOC容器与Spring.NET配置之比较
- spring.net IOC容器
- Castle IOC容器与Spring.NET配置之比较