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.使用配置文件
|
1
2
3
4
5
6
7
8
9
10
11
|
<!--出处:<a href="http://terrylee.cnblogs.com/">http://terrylee.cnblogs.com</a>--><?xml version="1.0" encoding="utf-8" ?><configuration> <components> <component id="comp1" lifestyle="transient"> <parameters> <para>component1 para</para> </parameters> </component>> </components></configuration> |
2.使用Attribute特性
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[Transient]public class MyComponent{ public MyComponent() { // } public MyComponent(string _Str) { // }} |
前面在Castle IOC的内幕故事中我们说过,组件生命方式是由一个叫做LifestyleModelInspector的Contributor来管理的。在LifestyleModelInspector中我们注意到有这样一段代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public virtual void ProcessModel(IKernel kernel, ComponentModel model){ if (!ReadLifestyleFromConfiguration(model)) { ReadLifestyleFromType(model); }}protected virtual bool ReadLifestyleFromConfiguration(ComponentModel model){ //}protected virtual void ReadLifestyleFromType(ComponentModel model){ //} |
其中ReadLifestyleFromConfiguration()从配置文件读取,ReadLifestyleFromType()是从组件的特性读取。可以看到LifestyleModelInspector首先会去检查配置文件中的是否指定,如果已经指定了,就会直接返回,否则才去组件特性里面去查找。由此我们可以得出如下一条重要的结论:
如果同时在配置文件和组件的特性中指定组件生命处理方式,配置文件将覆盖类中特性指定的。
二.自定义生命处理方式
下面我们来看如何实现自定义的生命处理方式。在这之前,先来看一下生命处理方式中的类结构图:

图1
可以看到,所有生命处理方式都实现了接口ILifestyleManager:
|
1
2
3
4
5
6
|
public interface ILifestyleManager : IDisposable{ void Init(IComponentActivator componentActivator, IKernel kernel); object Resolve(); void Release(object instance);} |
所以要实现自定义的生命处理方式,只要实现接口IlifestyleManager就可以了,来看一下Castle IOC官方网站提供的一种生命处理方式,实现了对于Web应用程序中的每一次Request都创建一个Singleton实例:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class PerWebRequestLifestyleManager : AbstractLifestyleManager{ private string PerRequestObjectID = "PerRequestLifestyleManager_" + Guid.NewGuid().ToString(); public override object Resolve() { if(HttpContext.Current.Items[PerRequestObjectID] == null) { // Create the actual object HttpContext.Current.Items[PerRequestObjectID] = base.Resolve(); } return HttpContext.Current.Items[PerRequestObjectID]; } public override void Dispose() { }} |
对于自定义的生命处理方式,在使用配置文件和特性指定的时候又有些不同
1.使用配置文件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!--出处:<a href="http://terrylee.cnblogs.com/">http://terrylee.cnblogs.com</a>--><?xml version="1.0" encoding="utf-8" ?><configuration> <components> <component id="myComponent" type="MyLib.MyComponent, MyLib" lifestyle="custom" customLifestyleType="MyLib.PerWebRequestLifestyleManager, MyLib"> <parameters> <para>component1 para</para> </parameters> </component> </components></configuration> |
2.使用Attribute特性
|
1
2
3
4
5
6
7
8
9
10
|
[CustomLifestyle( typeof(PerWebRequestLifestyleManager ) )]public class MyComponent{ public MyComponent() { // } //} |
三.生命周期管理
Castle IOC同样是支持组件生命周期的管理,也就是在组件装载,初始化,销毁所出发的行为,分别对应三个接口:IInitializable,ISupportInitialize,IDisposable。这些接口被分为两组:Commission和Decommission:
Commission
l Castle.Model.IInitializable interface
l System.ComponentModel.ISupportInitialize
Decommission
l System.IDisposable
如果组件实现了这些接口,容器会自动在不同的生命周期调用他们。我们看下面这样一个例子:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[Transient]public class MyComponent : IInitializable, IDisposable{ public MyComponent(string _para) { // } public void Initialize() { // } public void Dispose() { // }} |
在我们使用组件时
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class App{ public static void Main() { IWindsorContainer container = new WindsorContainer(new XmlInterpreter("../../BasicUsage.xml") ); container.AddComponent( "myComponent", typeof(MyComponent)); // Initialize()方法会自动执行 MyComponent instince = container["myComponent"] as MyComponent; // Dispose()方法会自动执行 container.Release(instince); }} |
关于Castle IOC容器组件生命周期管理就介绍到这里了。
Castle IOC容器组件生命周期管理的更多相关文章
- 【转】Tomcat组件生命周期管理
Tomcat组件生命周期管理 Tomcat中Server,Service,Connector,Engine,Host,Context,它们都实现了org.apache.catalina.Lifecyc ...
- Docker容器的生命周期管理
https://blog.csdn.net/u010278923/article/details/78751306
- Tomcat中组件的生命周期管理公共接口Lifecycle
Tomcat的组件都会实现一个Lifecycle接口,以方便组件的生命周期的统一管理 interface Lifecycle 组件生命周期中主要的几个方法 增加监听器,事件委托机制 public vo ...
- TOMCAT源码分析——生命周期管理
前言 从server.xml文件解析出来的各个对象都是容器,比如:Server.Service.Connector等.这些容器都具有新建.初始化完成.启动.停止.失败.销毁等状态.tomcat的实现提 ...
- 004-docker命令-容器生命周期管理、容器操作
1.容器生命周期管理 docker run :创建一个新的容器并运行一个命令 语法:docker run [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS说明: - ...
- Castle IOC容器内幕故事(下)
主要内容 1.ComponentModelBuilder 和 Contributors 2.Contributors分析 3.Handles分析 4.ComponentActivator分析 一.Co ...
- Castle IOC容器内幕故事(上)
主要内容 1.WindsorContainer分析 2.MicroKernel分析 3.注册组件流程 一.WindsorContainer分析 WindsorContainer是Castle的IOC容 ...
- Castle IOC容器与Spring.NET配置之比较
我本人对于Spring.NET并不了解,本文只是通过一个简单的例子来比较一下两者配置之间的区别.在Castle IOC容器中,提出了自动装配(Auto-Wiring)的概念,即由容器自动管理组件之间的 ...
- Castle IOC容器构建配置详解(二)
主要内容 1.基本类型配置 2.Array类型配置 3.List类型配置 4.Dictionary类型配置 5.自定义类型转换 一.基本类型配置 在Castle IOC的配置文件中,大家可能都已经注意 ...
随机推荐
- 最大熵模型 Maximum Entropy Model
熵的概念在统计学习与机器学习中真是很重要,熵的介绍在这里:信息熵 Information Theory .今天的主题是最大熵模型(Maximum Entropy Model,以下简称MaxEnt),M ...
- (六)6.16 Neurons Networks linear decoders and its implements
Sparse AutoEncoder是一个三层结构的网络,分别为输入输出与隐层,前边自编码器的描述可知,神经网络中的神经元都采用相同的激励函数,Linear Decoders 修改了自编码器的定义,对 ...
- linux 下RMAN备份shell脚本
RMAN备份对于Oracle数据库的备份与恢复简单易用,成本低廉.对于使用非catalog方式而言,将RMAN脚本嵌入到shell脚本,然后再通过crontab来实现中小型数据库数据库备份无疑是首选. ...
- centos mysq table is read only
1.进入mysql数据库目录,使用命令"chown -R mysql <数据库文件夹名称>" 2. "chgrp -R mysql <数据库文件夹名称& ...
- ECSide标签属性说明之<ec:table>
<ec:table>标签说明 ◆ 属性: tableId描述: 设置列表的唯一标识,默认为"ec",当一个页面内有多个ECSIDE列表时,必须为每个列表指定不同的tab ...
- 软件测试——boost单元测试 C++
分类: 1. 下载安装Boost 2. 在vs2010 中设置 工具->选项->vc++目录 设置包含文件目录:找到解压的boost文件夹eg:C:\boost_1_43_03. ...
- Struts2配置细节
struts.xml中 action中配置 如果是返回到网页则 /AA/XX.jsp 如果是返回到action则看namespace然后传参数,如果是同一个namespace则直接写上返回的actio ...
- Nodejs_day01
helloworld.txt的内容: 我是nodejs 阻塞式调用 var fs = require('fs'); var data = fs.readFileSync('helloworld.txt ...
- Trie树也称字典树
Trie树 Trie树也称字典树,因为其效率很高,所以在在字符串查找.前缀匹配等中应用很广泛,其高效率是以空间为代价的. 一.Trie树的原理 利用串构建一个字典树,这个字典树保存了串的公共前缀信息, ...
- MVC同一个视图多个submit对应不同action
Vi ew中代码 <input type="submit" value="yes" name="action"> <inp ...