主要内容

  1.配置什么

  2.几种配置方式

  3.Include 介绍

  4.Properties介绍

  5.条件状态

  一.配置什么

  Castle IOC中并不像Spring.net那样贯穿着一个思想就是一切皆为配置,对于对象之间的依赖关系,Castle IOC会自动去连接,因此相比之下它的配置文件要比Spring.net简单的多。我们主要配置的就是Component和Facility,所有的组件配置都放在Components节点中,每一个组件以<Component>开始,以</Component>结束,其中组件ID必须指定,组件的参数用< parameters >节点来指定:

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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>结束:

双击代码全选
1
2
3
4
5
6
<?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,简单的配置文件如下

双击代码全选
1
2
3
4
5
6
7
8
9
10
<?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配置区

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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

双击代码全选
1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <properties>
  <StrConn>MyConnectionString</StrConn>
 </properties>
<properties>
  <TimeOut>500</TimeOut>
 </properties>
</configuration>
双击代码全选
1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <facilities>
    <facility id="nhibernate" type="Full Type Name, AssemblyName"></facility>
  </facilities>
</configuration>

services.config

双击代码全选
1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <components>
  <component id="ILog"
   type="CastleDemo.DatabaseLog, CastleDemo" />
 </components>
</configuration>

  则在Web.config或者App.config中就可以这样去写了:

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
  </configSections>
  <castle>
    <include uri="<a href="file://properties.config/">file://properties.config</a>" />
    <include uri="<a href="file://facilities.config/">file://facilities.config</a>" />
    <include uri="<a href="file://services.config/">file://services.config</a>" />
  </castle>
</configuration>

  四.Properties介绍

  配置文件中,经常我们会遇到一个相同的值在多个地方使用,如数据库连接中的字符串,我们要是在每一个用到的地方都配置一遍,如果服务器变了,那所有的这些地方都得改动,这时可以使用Properties节点来配置,在容器使用配置信息之前,它将会被配置文件中的另一个Properties来替换,需要替换的地方我们使用#{}来标识。如

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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" />
双击代码全选
1
 
双击代码全选
1
2
3
4
5
<if defined="DEBUG">
  <interceptors>
    <interceptor>${logging.interceptor}</interceptor>
  </interceptors>
</if>
双击代码全选
1
choose
双击代码全选
1
2
3
4
5
6
<choose>
  <when defined="DEBUG">
    <component id="BasicUsageDebug" />
  </when>
  ……
</choose>
when 与choose结合使用
otherwise 与choose结合使用

  一个完整的配置示例如下:

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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 IOC容器构建配置详解(一)的更多相关文章

  1. Castle IOC容器构建配置详解(二)

    主要内容 1.基本类型配置 2.Array类型配置 3.List类型配置 4.Dictionary类型配置 5.自定义类型转换 一.基本类型配置 在Castle IOC的配置文件中,大家可能都已经注意 ...

  2. Spring源码解析二:IOC容器初始化过程详解

    IOC容器初始化分为三个步骤,分别是: 1.Resource定位,即BeanDefinition的资源定位. 2.BeanDefinition的载入 3.向IOC容器注册BeanDefinition ...

  3. Spring框架 之IOC容器 和AOP详解

    主要分析点: 一.Spring开源框架的简介  二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置  一.S ...

  4. Castle IOC容器与Spring.NET配置之比较

    我本人对于Spring.NET并不了解,本文只是通过一个简单的例子来比较一下两者配置之间的区别.在Castle IOC容器中,提出了自动装配(Auto-Wiring)的概念,即由容器自动管理组件之间的 ...

  5. Spring之IOC原理及代码详解

    一.什么是IOC 引用 Spring 官方原文:This chapter covers the Spring Framework implementation of the Inversion of ...

  6. Maven使用笔记(四)pom.xml配置详解

    pom.xml文件配置详解 --声明规范 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...

  7. maven常用插件配置详解

    常用插件配置详解Java代码    <!-- 全局属性配置 --> <properties> <project.build.name>tools</proje ...

  8. JAVAEE——spring01:介绍、搭建、概念、配置详解、属性注入和应用到项目

    一.spring介绍 1.三层架构中spring位置 2.spring一站式框架 正是因为spring框架性质是属于容器性质的. 容器中装什么对象就有什么功能.所以可以一站式. 不仅不排斥其他框架,还 ...

  9. 【转】Maven pom.xml 配置详解

    原文链接:https://yq.aliyun.com/articles/38271 pom.xml文件配置详解 --声明规范 <project xmlns="http://maven. ...

随机推荐

  1. (六)6.15 Neurons Networks Deep Belief Networks

    Hintion老爷子在06年的science上的论文里阐述了 RBMs 可以堆叠起来并且通过逐层贪婪的方式来训练,这种网络被称作Deep Belife Networks(DBN),DBN是一种可以学习 ...

  2. yeoman bower grunt等安装

    grunt-beginner前端自动化工具:http://www.imooc.com/learn/30 grunt的安装 官方站点:http://gruntjs.com/ 安装指令: sudo npm ...

  3. zabbix (2.0.6) 历史记录处乱码

    1.首先备份数据库 mysqldump -uroot -p123456 zabbix > zabbix.sql 2.设置字符 sed -i 's/latin1/utf8/g' zabbix.sq ...

  4. C#实现不安装Oracle客户端访问远程服务器数据!!

    概述: C#通过使用ADO的方式在未安装Oracle数据库的前提下,客户端程序远程访问服务器,会出现:“System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或 ...

  5. 【初识——最大流】 hdu 1532 Drainage Ditches(最大流) USACO 93

    最大流首次体验感受—— 什么是最大流呢? 从一个出发点(源点),走到一个目标点(汇点),途中可以经过若干条路,每条路有一个权值,表示这条路可以通过的最大流量. 最大流就是从源点到汇点,可以通过的最大流 ...

  6. webtest 文章

    一直也没有和游戏类测试打过交道,看到“腾讯WeTest ”提供的测试服务,以及和手机游戏相关的技术文章.在此作个备份记录的. 手游专题 http://wetest.qq.com/lab/tag/?ta ...

  7. 【C++】统计代码覆盖率(二)

    嗷嗷嗷!!!好激动,我好蠢.不过最后还是解决了.呜呜呜 有些都是东一块西一块查的,如果有侵权欢迎私信我,我注明出处. 一 gcov&CMake 昨天试了下测试代码和被测代码都是c++的情况,直 ...

  8. Maven安装testNG

    1.Maven安装testNG (1)打开网站:http://testng.org/doc/maven.html (2)复制如下代码,粘贴到项目的pom.xml文件: 1 <dependency ...

  9. JavaScript中的Function(函数)对象

    1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document->html->(head,body) 4. ...

  10. 在Jenkins中使用Git Plugin访问Https代码库失败的问题

    最近需要在Jenkins上配置一个Job,SCM源是http://git.opendaylight.org/gerrit/p/integration.git 于是使用Jenkins的Git Plugi ...