Castle IOC容器构建配置详解(一)

简介:
摘要:知道如何简单使用Castle IOC,也听完了Castle IOC的内幕故事,从本文开始讲会详细讲解Castle IOC的一些使用。先从构建配置信息开始吧,在Castle IOC中并不像Spring.net那样贯穿着一个思想就是一切皆为配置,对于对象之间的依赖关系,Castle IOC会自动去连接,因此相比之下它的配置文件要比Spring.net简单的多。
 
主要内容
1 .配置什么
2 .几种配置方式
3 Include 介绍
4 Properties介绍
5 .条件状态
 
一.配置什么
Castle IOC 中并不像Spring.net那样贯穿着一个思想就是一切皆为配置,对于对象之间的依赖关系,Castle IOC会自动去连接,因此相比之下它的配置文件要比Spring.net简单的多。我们主要配置的就是ComponentFacility,所有的组件配置都放在Components节点中,每一个组件以<Component>开始,以</Component>结束,其中组件ID必须指定,组件的参数用<   parameters > 节点来指定:
<!-- 出处:[url]http://terrylee.cnblogs.com[/url] -->

<? xml version="1.0" encoding="utf-8"  ?>

< configuration >

    
< components >

        
< component  id ="comp1" >

            
< parameters >

                
< para > component1 para </ para >

            
</ parameters >

        
</ component >

        
< component  id ="comp2" >

            
< parameters >

                
< para > component2 para </ para >

            
</ parameters >

        
</ component >

    
</ components >

</ configuration >
所有的扩展单元配置都在 Facilities节点中,每一个扩展单元以一个 <Facility>开始,以 </Facility>结束:
<!-- 出处:[url]http://terrylee.cnblogs.com[/url] -->

<? xml version="1.0" encoding="utf-8"  ?>

< configuration >

    
< facilities >

        
< facility  id ="nhibernate"  type ="Full Type Name, AssemblyName" ></ facility >

        
< facility  id ="transaction"  type ="Full Type Name, AssemblyName" ></ facility >

    
</ facilities >

</ configuration >
二.几种配置方式
Castle IOC中,支持三种方式的配置
l          XML 方式的配置
l          应用程序配置文件
l          实现接口IconfigurationStore自定义配置
1 XML方式的配置
指定一个自定义的XML作为配置文件,在实例化容器的时候用 XmlInterpreter ,简单的配置文件如下
<!-- 出处:[url]http://terrylee.cnblogs.com[/url] -->

<? xml version="1.0" encoding="utf-8"  ?>

< configuration >

    
< components >

        
< component  id ="txtLog" >

            
< parameters >

                
< target > log.txt </ target >

            
</ parameters >

        
</ component >

    
</ components >

</ configuration >
初始化容器时的代码
IWindsorContainer container  =   new  WindsorContainer(  new  XmlInterpreter( " BasicUsage.xml " ) );
2 .使用应用程序配置文件
Web.config或者App.config作为配置文件,这种方式的配置在实例化时不需要指定配置文件,容器会自动去检查应用程序的配置文件中的Castle IOC配置区
<!-- 出处:[url]http://terrylee.cnblogs.com[/url] -->

<? xml version="1.0" encoding="utf-8"  ?>

< configuration >

    
< configSections >

        
< section  name ="castle"  type ="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,Castle.Windsor"   />

    
</ configSections >

    
< castle >

        
< components >

            
< component  id ="txtLog" >

                
< parameters >

                    
< target > log.txt </ target >

                
</ parameters >

            
</ component >

        
</ components >

    
</ castle >

</ configuration >
初始化容器时直接采用默认初始化
IWindsorContainer container  =   new  WindsorContainer( new  XmlInterpreter() );
  三.Include介绍
有时系统比较复杂,组件会非常的多。如果把这么多的组件放在了同一个配置文件里面去配置,配置文件将变得很大,而且不易阅读。使用Include可以把配置文件分开在很多个不同的配置文件中,最后统一在一个XML中或者应用程序配置文件指定这些分开配置文件的URI。如下面所示,我们有这样三个配置文件:
properties.config
<!-- 出处:[url]http://terrylee.cnblogs.com[/url] -->

<? xml version="1.0" encoding="utf-8"  ?>

< configuration >

  
< properties >

    
< StrConn > MyConnectionString </ StrConn >

  
</ properties >

< properties >

    
< TimeOut > 500 </ TimeOut >

  
</ properties >

</ configuration >
facilities.config
<!-- 出处:[url]http://terrylee.cnblogs.com[/url] -->

<? xml version="1.0" encoding="utf-8"  ?>

< configuration >

    
< facilities >

        
< facility  id ="nhibernate"  type ="Full Type Name, AssemblyName" ></ facility >

    
</ facilities >

</ configuration >
services.config
<!-- 出处:[url]http://terrylee.cnblogs.com[/url] -->

<? xml version="1.0" encoding="utf-8"  ?>

< configuration >

  
< components >

    
< component  id ="ILog"  

      type
="CastleDemo.DatabaseLog, CastleDemo"   />

  
</ components >

</ configuration >
则在 Web.config或者 App.config中就可以这样去写了:
<!-- 出处:[url]http://terrylee.cnblogs.com[/url] -->

<? xml version="1.0" encoding="utf-8"  ?>

< configuration >

    
< configSections >

        
< section  name ="castle"  type ="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"   />

    
</ configSections >

    
< castle >

        
< include  uri ="file://properties.config"   />

        
< include  uri ="file://facilities.config"   />

        
< include  uri ="file://services.config"   />

    
</ castle >

</ configuration >
 四.Properties介绍  
配置文件中,经常我们会遇到一个相同的值在多个地方使用,如数据库连接中的字符串,我们要是在每一个用到的地方都配置一遍,如果服务器变了,那所有的这些地方都得改动,这时可以使用Properties节点来配置,在容器使用配置信息之前,它将会被配置文件中的另一个Properties来替换,需要替换的地方我们使用#{}来标识。如
<!-- 出处:[url]http://terrylee.cnblogs.com[/url] -->

<? xml version="1.0" encoding="utf-8"  ?>

< configuration >

    
< properties >

        
< connectionString > server=local;uid=sa;pwd=sa;database=test </ connectionString >

    
</ properties >

    
< components >

        
< component  id ="connectionfactory"  service ="Company.Project.IConnectionFactory, Company.Project"

            type
="Company.Project.DefaultConnectionFactory, Company.Project" >

            
< properties >

                
< connString > #{connectionString} </ connString >

            
</ properties >

        
</ component >

    
</ components >

</ configuration >
五.条件状态
配置文件有时候会非常的复杂,Castle IOC支持我们在配置文件中使用条件选择或者判断,这有点像C#预处理指令(如#if,如果你没有使用过,可以参考MSDN),可以使用以下的节点:
名称
示例
define
< define   flag ="DEBUG"   />
undef
< undef   flag ="DEBUG"   />
if
< if   defined ="DEBUG">
    <interceptors>
        <interceptor>${logging.interceptor}</interceptor>
    </interceptors>
</ if >
choose
< choose >
    <when defined="DEBUG">
        <component id="BasicUsageDebug" />
    </when>
     ……
</ choose >
when
choose结合使用
otherwise
choose结合使用
一个完整的配置示例如下:
<!-- 出处:[url]http://terrylee.cnblogs.com[/url] -->

<? xml version="1.0" encoding="utf-8"  ?>

< configuration >

    
< define  flag ="DEBUG"   />

    
< components >

        
< component  id ="BasicUsageDemo" >

            
< if  defined ="DEBUG" >

                
< interceptors >

                    
< interceptor > ${logging.interceptor} </ interceptor >

                
</ interceptors >

            
</ if >

        
</ component >

        
< undef  flag ="DEBUG"   />

        
< choose >

            
< when  defined ="DEBUG" >

                
< component  id ="BasicUsageDebug"   />

            
</ when >

            
< when  defined ="Complex" >

                
< component  id ="ComplexDeme"   />

            
</ when >

            
< when  defined ="Prod" >

                
< component  id ="ProdDemo"   />

            
</ when >

            
< otherwise >

                
< component  id ="Default"   />

            
</ otherwise >

        
</ choose >

    
</ components >

</ configuration >

Castle IOC
容器的基本配置就到这儿了,在下一篇中会介绍一些复杂类型的配置及类型转换。
 
参考资料
Castle 的官方网站[url]http://www.castleproject.org[/url]

















本文转自lihuijun51CTO博客,原文链接:  http://blog.51cto.com/terrylee/67676 ,如需转载请自行联系原作者
相关文章
|
8月前
|
域名解析 网络协议 API
【Azure Container App】配置容器应用的缩放规则 Managed Identity 连接中国区 Azure Service Bus 问题
本文介绍了在 Azure Container Apps 中配置基于自定义 Azure Service Bus 的自动缩放规则时,因未指定云环境导致的域名解析错误问题。解决方案是在扩展规则中添加 `cloud=AzureChinaCloud` 参数,以适配中国区 Azure 环境。内容涵盖问题描述、原因分析、解决方法及配置示例,适用于使用 KEDA 实现事件驱动自动缩放的场景。
182 1
|
5月前
|
XML Java 测试技术
《深入理解Spring》:IoC容器核心原理与实战
Spring IoC通过控制反转与依赖注入实现对象间的解耦,由容器统一管理Bean的生命周期与依赖关系。支持XML、注解和Java配置三种方式,结合作用域、条件化配置与循环依赖处理等机制,提升应用的可维护性与可测试性,是现代Java开发的核心基石。
|
7月前
|
缓存 Ubuntu Docker
Ubuntu环境下删除Docker镜像与容器、配置静态IP地址教程。
如果遇见问题或者想回滚改动, 可以重启系统.
463 16
|
7月前
|
数据建模 应用服务中间件 PHP
配置nginx容器和php容器协同工作成功,使用ip加端口的方式进行通信
本示例演示如何通过Docker挂载同一宿主目录至Nginx与PHP容器,实现PHP项目运行环境配置。需注意PHP容器中监听地址修改为0.0.0.0:9000,并调整Nginx配置中fastcgi_pass指向正确的IP与端口。同时确保Nginx容器中/var/www/html权限正确,以避免访问问题。
配置nginx容器和php容器协同工作成功,使用ip加端口的方式进行通信
|
10月前
|
XML Java 数据格式
Spring IoC容器的设计与实现
Spring 是一个功能强大且模块化的 Java 开发框架,其核心架构围绕 IoC 容器、AOP、数据访问与集成、Web 层支持等展开。其中,`BeanFactory` 和 `ApplicationContext` 是 Spring 容器的核心组件,分别定位为基础容器和高级容器,前者提供轻量级的 Bean 管理,后者扩展了事件发布、国际化等功能。
260 18
|
XML Java 数据格式
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
|
7月前
|
Kubernetes Docker Python
Docker 与 Kubernetes 容器化部署核心技术及企业级应用实践全方案解析
本文详解Docker与Kubernetes容器化技术,涵盖概念原理、环境搭建、镜像构建、应用部署及监控扩展,助你掌握企业级容器化方案,提升应用开发与运维效率。
1050 108
|
8月前
|
存储 监控 测试技术
如何将现有的应用程序迁移到Docker容器中?
如何将现有的应用程序迁移到Docker容器中?
607 57