Castle IOC容器组件生命周期管理
2006-04-26 08:24
417 查看
摘要:Castle IOC容器为我们提供了不同的生命处理方式以及组件生命周期的处理,即组件装载,初始化,销毁时所表现的行为。如何去使用它们?本文讲对这一内容进行详细的阐述。
主要内容[/b]
1.生命处理方式
2.自定义生命处理方式
3.生命周期处理
一.生命处理方式[/b]
我们通常创建一个组件的实例使用new关键字,这样每次创建出来的都是一个新的实例,如果想要组件只有一个实例,我们会使用Singleton模式。在Castle IOC中,它支持我们对于组件的实例进行控制,也就是说我们可以透明的管理一个组件拥有多少个实例。Castle IOC容器提供了如下几种生命处理方式:
l Singleton:一个组件只有一个实例被创建,所有请求的客户使用程序得到的都是同一个实例,同时这也是Castle IOC容器默认的一种处理方式。
l Transient:这种处理方式与我们平时使用new的效果是一样的,对于每次的请求得到的都是一个新的实例。
l PerThread:对于每一个线程来说是使用了Singleton,也就是每一个线程得到的都是同一个实例。
l Pooled:对象池的处理方式,对于不再需用的实例会保存到一个对象池中。
l Custom:自定义的生命处理方式。
我们可以通过以下两种方式来指定组件的生命处理方式,如果不指定,则为Singleton方式:
1.使用配置文件
<!--出处:http://terrylee.cnblogs.com-->
<?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特性
//出处:http://terrylee.cnblogs.com
[Transient]
public class MyComponent
public virtual void ProcessModel(IKernel kernel, ComponentModel model)
protected virtual bool ReadLifestyleFromConfiguration(ComponentModel model)
protected virtual void ReadLifestyleFromType(ComponentModel model)
public interface ILifestyleManager : IDisposable
public class PerWebRequestLifestyleManager : AbstractLifestyleManager
<!--出处:http://terrylee.cnblogs.com-->
<?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特性
//出处:http://terrylee.cnblogs.com
[CustomLifestyle( typeof(PerWebRequestLifestyleManager ) )]
public class MyComponent
//出处:http://terrylee.cnblogs.com
[Transient]
public class MyComponent : IInitializable, IDisposable
//出处:http://terrylee.cnblogs.com
public class App
{
public static void Main()
{
IWindsorContainer container = new WindsorContainer(new XmlInterpreter("http://www.cnblogs.com/BasicUsage.xml") );
container.AddComponent( "myComponent",
typeof(MyComponent));
// Initialize()方法会自动执行
MyComponent instince = container["myComponent"] as MyComponent;
// Dispose()方法会自动执行
container.Release(instince);
}
}
关于Castle IOC容器组件生命周期管理就介绍到这里了。
参考资料[/b]
Castle的官方网站http://www.castleproject.org
主要内容[/b]
1.生命处理方式
2.自定义生命处理方式
3.生命周期处理
一.生命处理方式[/b]
我们通常创建一个组件的实例使用new关键字,这样每次创建出来的都是一个新的实例,如果想要组件只有一个实例,我们会使用Singleton模式。在Castle IOC中,它支持我们对于组件的实例进行控制,也就是说我们可以透明的管理一个组件拥有多少个实例。Castle IOC容器提供了如下几种生命处理方式:
l Singleton:一个组件只有一个实例被创建,所有请求的客户使用程序得到的都是同一个实例,同时这也是Castle IOC容器默认的一种处理方式。
l Transient:这种处理方式与我们平时使用new的效果是一样的,对于每次的请求得到的都是一个新的实例。
l PerThread:对于每一个线程来说是使用了Singleton,也就是每一个线程得到的都是同一个实例。
l Pooled:对象池的处理方式,对于不再需用的实例会保存到一个对象池中。
l Custom:自定义的生命处理方式。
我们可以通过以下两种方式来指定组件的生命处理方式,如果不指定,则为Singleton方式:
1.使用配置文件
<!--出处:http://terrylee.cnblogs.com-->
<?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特性
//出处:http://terrylee.cnblogs.com
[Transient]
public class MyComponent
public virtual void ProcessModel(IKernel kernel, ComponentModel model)
protected virtual bool ReadLifestyleFromConfiguration(ComponentModel model)
protected virtual void ReadLifestyleFromType(ComponentModel model)
public interface ILifestyleManager : IDisposable
public class PerWebRequestLifestyleManager : AbstractLifestyleManager
<!--出处:http://terrylee.cnblogs.com-->
<?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特性
//出处:http://terrylee.cnblogs.com
[CustomLifestyle( typeof(PerWebRequestLifestyleManager ) )]
public class MyComponent
//出处:http://terrylee.cnblogs.com
[Transient]
public class MyComponent : IInitializable, IDisposable
//出处:http://terrylee.cnblogs.com
public class App
{
public static void Main()
{
IWindsorContainer container = new WindsorContainer(new XmlInterpreter("http://www.cnblogs.com/BasicUsage.xml") );
container.AddComponent( "myComponent",
typeof(MyComponent));
// Initialize()方法会自动执行
MyComponent instince = container["myComponent"] as MyComponent;
// Dispose()方法会自动执行
container.Release(instince);
}
}
关于Castle IOC容器组件生命周期管理就介绍到这里了。
参考资料[/b]
Castle的官方网站http://www.castleproject.org
相关文章推荐
- Castle IOC容器组件生命周期管理
- Castle IOC容器组件生命周期管理
- Castle IOC容器组件生命周期管理
- Tomcat6源代码学习--容器组件通讯机制与生命周期管理
- 【Spring】1 Spring IoC容器中组件管理 基础
- 解决容器对组件的“侵入式”管理方法之二——控制反转(Ioc)
- IoC容器10—— Classpath扫描和组件管理
- 【SSH进阶之路】一步步重构容器实现Spring框架——解决容器对组件的“侵入式”管理的两种方案--主动查找和控制反转(九)
- Android核心基础-8.Android四大组件之Activity-3.生命周期、横竖屏切换、启动模式、进程管理
- Android进程线程管理机制与组件生命周期解析
- IOC容器中bean的生命周期
- 通过在classpath自动扫描方式把组件纳入spring容器中管理。
- spring容器与管理bean的生命周期
- Castle IOC容器构建配置详解(一)
- Castle IOC容器实践之EnterpriseLibrary Configuration Facility
- 《Spring 3.x 企业应用开发实战》学习笔记 第三章 IoC容器概述 3.5 Bean的生命周期
- Ioc容器Autofac系列(3)-- 三种注册组件的方式
- Castle IOC容器构建配置详解(一)
- Castle IOC容器实践之TypedFactory Facility(二)
- spring+springMVC 事物管理陷阱, springMVC 加载组件会覆盖父容器组件