zoukankan      html  css  js  c++  java
  • 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
    <?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
    <?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容器组件生命周期管理就介绍到这里了。

  • 相关阅读:
    电影记录管理系统 修改与注释,完整代码
    Mybatis用法小结
    springMVC中传值的时候的乱码问题
    MAVEN安装过程
    树形结构的数据库表Schema设计
    SpringMVC的工作原理
    页面底部的回到顶部的按钮实现
    鼠标放上去,div高度随文字增加,并显示剩余的文字。
    freeMarker中list的两列展示
    html的textarea控制字数小案例
  • 原文地址:https://www.cnblogs.com/lvfeilong/p/Castle-9.html
Copyright © 2011-2022 走看看