Castle IOC容器内幕故事(上)

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介:


摘要:在快速入门指南篇中,我们对于Castle IOC容器的使用已经有了一个直观的认识。本文将在这基础上进一步对Castle IOC容器的结构及其注册组件的过程做一个深入的分析,让我们开始Castle IOC的内幕故事吧。

 
主要内容
1 WindsorContainer分析
2 MicroKernel分析
3 .注册组件流程
 
一.WindsorContainer分析
WindsorContainer CastleIOC容器,也是它的一个核心,先来看一下WindsorContainerCastle中所处的位置:
1
WindsorContainer 构建于 MicroKernel 之上, MicroKernel 仅仅是提供了一个IOC的容器,非常的轻巧,它只依赖于 Castle.Model 一个程序集,但它的可扩展能力却很强,后面会讲到 ;可以这么理解, WindsorContainer 为我们提供了一个Façade,它封装了MicroKernel,并且提供了一些扩展点,但 它的核心仍然是Microkernel 如下图所示:
2
二.MicroKernel分析
既然MicroKernel WindsorContainer 的核心,那我们就来看一下 MicroKernel 的结构,从下面的结构图中,可以看到MicroKernel的组成主要有SubSystemComponentsFacilities几个部分,SubSystem主要用来处理一些扩展功能,如配置、类型转换等,我们也可以实现自己的SubSystemComponents称为组件,在快速入门指南中我已经提到了,这里再说一下, 服务是一个个的接口,接口约定了服务,从而使随意替换服务的实现对使用接口服务的代码没有任何的影响,组件是一个可重用的程序单元,它实现了某个接口,并仅仅只实现了这一个良好的接口,也就是说,组件是实现了某个服务接口的类;Facilities我们称之为扩张单元,如果我们想扩张容器的功能,可以通过创建扩张单元来实现,我们可以在扩张单元里面订阅容器事件,给组件附加属性,建立拦截器,控制组件生命周期等,扩张单元是以一种插件的形式存在的,所以非常便于扩展,可以编写自己的扩张单元,后面我会写Castle自带的一些扩张单元的使用。MicroKernel的结构如下图:
3
三.注册组件流程
现在我们来看一下当注册一个组件时,容器做了什么?
public   virtual   void  AddComponent(String key, Type classType)

{

    _kernel.AddComponent(key, classType);

}


 

public   virtual   void  AddComponent(String key, Type serviceType, Type classType)

{

    _kernel.AddComponent(key, serviceType, classType);

}


//   [url]http://terrylee.cnblogs.com[/url]

可以看到, WindsorContainer 仅仅是调用了MicroKernel的方法来完成组件的注册,它只是对MicroKernel做了一次封装,核心的功能都由MicroKernel来完成,看一下MicroKernel中的AddComponent()方法的实现
public   virtual   void  AddComponent(String key, Type classType)

{

    
if (key == nullthrow new ArgumentNullException("key");

    
if (classType == nullthrow new ArgumentNullException("classType");

 

    ComponentModel model 
= ComponentModelBuilder.BuildModel(key, classType, classType, null);

    RaiseComponentModelCreated(model);

    IHandler handler 
= HandlerFactory.Create(model);

    RegisterHandler(key, handler);

}


 

public   virtual   void  AddComponent(String key, Type serviceType, Type classType)

{

    
if (key == nullthrow new ArgumentNullException("key");

    
if (serviceType == nullthrow new ArgumentNullException("serviceType");

    
if (classType == nullthrow new ArgumentNullException("classType");

 

    ComponentModel model 
= ComponentModelBuilder.BuildModel(key, serviceType, classType, null);

    RaiseComponentModelCreated(model);

    IHandler handler 
= HandlerFactory.Create(model);

    RegisterHandler(key, handler);

}


//   [url]http://terrylee.cnblogs.com[/url]
先做一些必要的异常处理,然后为当前组件创建 ComponentModel 实例,ComponentModel获取当前组件的详细元信息,而且这个信息在容器中的任何地方都可以使用,所以ComponentModel其实就是组件的一个“元信息库”。创建ComponentModel的过程如下:
public  ComponentModel BuildModel(String key, Type service, 

    Type classType, IDictionary extendedProperties)

{

    ComponentModel model 
= new ComponentModel(key, service, classType);


    
if (extendedProperties != null)

    
{

        model.ExtendedProperties 
= extendedProperties;

    }


    
foreach(IContributeComponentModelConstruction contributor in contributors)

    
{

        contributor.ProcessModel( kernel, model );

    }


    
return model;
}

//   [url]http://terrylee.cnblogs.com[/url]

创建 ComponentModel 的过程其实就是调用contributor来对组件进行处理,它会按照顺序对添加进来的contributor依次调用,在DefaultComponentModelBuilder一共注册了八个Contributor,分别为:
protected   virtual   void  InitializeContributors()

{

    AddContributor( 
new ConfigurationModelInspector() );

    AddContributor( 
new LifestyleModelInspector() );

    AddContributor( 
new ConstructorDependenciesModelInspector() );

    AddContributor( 
new PropertiesDependenciesModelInspector() );

    AddContributor( 
new MethodMetaInspector() );

    AddContributor( 
new LifecycleModelInspector() );

    AddContributor( 
new ConfigurationParametersInspector() );

    AddContributor( 
new InterceptorInspector() );

}


//   [url]http://terrylee.cnblogs.com[/url]
这八个 Contributor形成了一个处理组件的流程,它们涵盖了组件处理流程中的配置,生命周期,构造函数依赖,属性依赖等方面,每一个 Contributor只负责某一方面的事情。再下来一步就是发出 ComponentModelCreated事件了,这步的操作很简单
protected   virtual   void  RaiseComponentModelCreated(ComponentModel model)

{

    ComponentModelDelegate eventDelegate 
= (ComponentModelDelegate) events[ComponentModelCreatedEvent];

    
if (eventDelegate != null) eventDelegate(model);

}


//   [url]http://terrylee.cnblogs.com[/url]
现在 ComponentModel创建完成,该是创建 IHandler了, IHandler并不做创建组件的工作,它主要的功能是创建 ComponentActivator,而 ComponentActivator则是完成容器的组件创建工作,它首先会根据 ComponentModel“信息库”检查相关的依赖,检查通过后根据生命周期管理来创建不同类型的组件,创建 DefaultHandler的代码如下:
public   virtual  IHandler Create(ComponentModel model)

{

    IHandler handler 
= new DefaultHandler(model);

    handler.Init(kernel);

    
return handler;

}


//   [url]http://terrylee.cnblogs.com[/url]
最后发出 ComponentRegistered HandlerRegistered事件,完成整个组件的注册过程。
参考资料

Castle的官方网站[url]http://www.castleproject.org[/url]















本文转自lihuijun51CTO博客,原文链接:http://blog.51cto.com/terrylee/67672 ,如需转载请自行联系原作者

相关文章
|
29天前
|
XML Java 测试技术
《深入理解Spring》:IoC容器核心原理与实战
Spring IoC通过控制反转与依赖注入实现对象间的解耦,由容器统一管理Bean的生命周期与依赖关系。支持XML、注解和Java配置三种方式,结合作用域、条件化配置与循环依赖处理等机制,提升应用的可维护性与可测试性,是现代Java开发的核心基石。
|
XML Java 数据格式
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
|
6月前
|
XML Java 数据格式
Spring IoC容器的设计与实现
Spring 是一个功能强大且模块化的 Java 开发框架,其核心架构围绕 IoC 容器、AOP、数据访问与集成、Web 层支持等展开。其中,`BeanFactory` 和 `ApplicationContext` 是 Spring 容器的核心组件,分别定位为基础容器和高级容器,前者提供轻量级的 Bean 管理,后者扩展了事件发布、国际化等功能。
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
XML Java 开发者
经典面试---spring IOC容器的核心实现原理
作为一名拥有十年研发经验的工程师,对Spring框架尤其是其IOC(Inversion of Control,控制反转)容器的核心实现原理有着深入的理解。
725 3
|
XML Java 数据格式
Spring IOC容器的深度解析及实战应用
【10月更文挑战第14天】在软件工程中,随着系统规模的扩大,对象间的依赖关系变得越来越复杂,这导致了系统的高耦合度,增加了开发和维护的难度。为解决这一问题,Michael Mattson在1996年提出了IOC(Inversion of Control,控制反转)理论,旨在降低对象间的耦合度,提高系统的灵活性和可维护性。Spring框架正是基于这一理论,通过IOC容器实现了对象间的依赖注入和生命周期管理。
238 0
|
XML Java 数据格式
Spring5入门到实战------8、IOC容器-Bean管理注解方式
这篇文章详细介绍了Spring5框架中使用注解进行Bean管理的方法,包括创建Bean的注解、自动装配和属性注入的注解,以及如何用配置类替代XML配置文件实现完全注解开发。
Spring5入门到实战------8、IOC容器-Bean管理注解方式
|
3月前
|
Kubernetes Docker Python
Docker 与 Kubernetes 容器化部署核心技术及企业级应用实践全方案解析
本文详解Docker与Kubernetes容器化技术,涵盖概念原理、环境搭建、镜像构建、应用部署及监控扩展,助你掌握企业级容器化方案,提升应用开发与运维效率。
711 108