摘要:Castle IOC容器为我们提供了不同的生命处理方式以及组件生命周期的处理,即组件装载,初始化,销毁时所表现的行为。如何去使用它们?本文讲对这一内容进行详细的阐述。
主要内容
1
.生命处理方式
2
.自定义生命处理方式
3
.生命周期处理
一.生命处理方式
我们通常创建一个组件的实例使用new关键字,这样每次创建出来的都是一个新的实例,如果想要组件只有一个实例,我们会使用Singleton模式。在Castle IOC中,它支持我们对于组件的实例进行控制,也就是说我们可以透明的管理一个组件拥有多少个实例。Castle IOC容器提供了如下几种生命处理方式:
l
Singleton
:一个组件只有一个实例被创建,所有请求的客户使用程序得到的都是同一个实例,同时这也是Castle IOC容器默认的一种处理方式。
l
Transient
:这种处理方式与我们平时使用new的效果是一样的,对于每次的请求得到的都是一个新的实例。
l
PerThread
:对于每一个线程来说是使用了Singleton,也就是每一个线程得到的都是同一个实例。
l
Pooled
:对象池的处理方式,对于不再需用的实例会保存到一个对象池中。
l
Custom
:自定义的生命处理方式。
我们可以通过以下两种方式来指定组件的生命处理方式,如果不指定,则为Singleton方式:
1
.使用配置文件
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
.使用Attribute特性
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
前面在
Castle IOC的内幕故事中我们说过,组件生命方式是由一个叫做
LifestyleModelInspector
的Contributor来管理的。在LifestyleModelInspector中我们注意到有这样一段代码:
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
其中
ReadLifestyleFromConfiguration()
从配置文件读取,ReadLifestyleFromType()是从组件的特性读取。可以看到LifestyleModelInspector首先会去检查配置文件中的是否指定,如果已经指定了,就会直接返回,否则才去组件特性里面去查找。由此我们可以得出如下一条重要的结论:
如果同时在配置文件和组件的特性中指定组件生命处理方式,配置文件将覆盖类中特性指定的。
二.自定义生命处理方式
下面我们来看如何实现自定义的生命处理方式。在这之前,先来看一下生命处理方式中的类结构图:
![](https://i-blog.csdnimg.cn/blog_migrate/21927a7942d4ba2fc6c1ed07db7099ff.png)
可以看到,所有生命处理方式都实现了接口
ILifestyleManager
:
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
所以要实现自定义的生命处理方式,只要实现接口
IlifestyleManager
就可以了,来看一下Castle IOC官方网站提供的一种生命处理方式,实现了对于Web应用程序中的每一次Request都创建一个Singleton实例:
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
对于自定义的生命处理方式,在使用配置文件和特性指定的时候又有些不同
1
.使用配置文件
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
.使用Attribute特性
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
三.生命周期管理
Castle IOC
同样是支持组件生命周期的管理,也就是在组件装载,初始化,销毁所出发的行为,分别对应三个接口:IInitializable,ISupportInitialize,IDisposable。这些接口被分为两组:Commission和Decommission:
Commission
l
Castle.Model.IInitializable interface
l
System.ComponentModel.ISupportInitialize
Decommission
l
System.IDisposable
如果组件实现了这些接口,容器会自动在不同的生命周期调用他们。我们看下面这样一个例子:
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
在我们使用组件时
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
关于 Castle IOC容器组件生命周期管理就介绍到这里了。
本文转自lihuijun51CTO博客,原文链接: http://blog.51cto.com/terrylee/67683
,如需转载请自行联系原作者