castle windsor学习-----Registering components in XML 使用XML配置注入

1.Components are defined by tag <component/> within <components/> section.

<components>
  <component ...attributes>
    ...elements
  </component>
</components>

 

例子:

<components>
  <component
      id="notification"
      type="Acme.Crm.Services.EmailNotificationService, Acme.Crm">
  </component>
</components>

 

 id:组件的名称,type:组件的类型

<components>
  <component
      id="notification"
      service="Acme.Crm.Services.INotificationService, Acme.Crm"
      type="Acme.Crm.Services.EmailNotificationService, Acme.Crm">
  </component>
</components>

 

类似于:

Component.For<INotificationService>().ImplementedBy<EmailNotificationService>().Named("notification")

 

带有生命周期的组件

<components>
  <component
      id="notification"
      service="Acme.Crm.Services.INotificationService, Acme.Crm"
      type="Acme.Crm.Services.EmailNotificationService, Acme.Crm"
      lifestyle="transient">
  </component>
</components>

 

带有参数的组件

<components>
 <component type="emailFormatter"></component>
<component id="notification" service="Acme.Crm.Services.INotificationService, Acme.Crm" type="Acme.Crm.Services.EmailNotificationService, Acme.Crm" lifestyle="transient"> <parameters> <smtpServer>localhost:667</smtpServer> <senderEmail>#{admin.email}</senderEmail> <emailFormatter>${emailFormatter}</emailFormatter> </parameters> </component> </components>
<properties>
    <admin.email>666</admin.email>
</properties>
 

 

<parameters>可以是构造函数参数或者是属性,名称要保持一致 

在xml先定义好<properties>节点,存放的是各类参数名称

<properties>
    <admin.email>666</admin.email>
</properties>

 

如果参数是其他服务,则要用${}方法引用服务   

一个组件多个服务

<components>
  <component
      id="notification"
      service="Acme.Crm.Services.INotificationService, Acme.Crm"
      type="Acme.Crm.Services.EmailNotificationService, Acme.Crm">
    <forwardedTypes>
      <add service="Acme.Crm.Services.IEmailSender, Acme.Crm" />
    </forwardedTypes>
  </component>
</components>

 

posted @ 2017-03-11 17:53  蓝平凡  阅读(392)  评论(0编辑  收藏  举报