您的位置:首页 > 编程语言 > Java开发

『Spring.Net』IoC 容器

2012-02-01 10:50 260 查看
 

 

什么是IoC

 



 

如何使用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

typeSection 5.2.5, “Instantiating objects”
id and nameSection 5.2.4.1, “Naming objects”
singleton or prototypeSection 5.4, “Object Scopes”
object propertiesSection 5.3.1, “Dependency injection”
constructor argumentsSection 5.3.1, “Dependency injection”
autowiring modeSection 5.3.6, “Autowiring collaborators”
dependency checking modeSection 5.3.7, “Checking for dependencies”
initialization methodSection 5.6.1, “Lifecycle interfaces”
destruction methodSection 5.6.1, “Lifecycle interfaces”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: