关于Castle IOC容器自动装配的问题

今天有一个“27745754”的朋友对Castle IOC容器的自动装配提出了疑问,如果有多个类(组件)实现同一个接口(服务),它会自动选择哪个来进行装配?很多朋友应该都有这样的问题,这里特别说明一下。

还是以我在Castle IOC容器快速入门里用到的日志记录组件为例,现在我们对于ILogFormatter接口:

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
InBlock.gif
/// Author:Terrylee
InBlock.gif
InBlock.gif
/// From:http://terrylee.cnblogs.com
InBlock.gif
ExpandedBlockEnd.gif
/// </summary>

None.gif
None.gif
public   interface  ILogFormatter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
string Format(string MsgStr);
ExpandedBlockEnd.gif}

它有两个实现,分别为:TextFormatterPlanFormatter

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
InBlock.gif
/// Author:Terrylee
InBlock.gif
InBlock.gif
/// From:http://terrylee.cnblogs.com
InBlock.gif
ExpandedBlockEnd.gif
/// </summary>
None.gif
None.gif
public   class  TextFormatter : ILogFormatter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public TextFormatter()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public string Format(string MsgStr)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return "[" + MsgStr + "]";
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

 

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
InBlock.gif
/// Author:Terrylee
InBlock.gif
InBlock.gif
/// From:http://terrylee.cnblogs.com
InBlock.gif
ExpandedBlockEnd.gif
/// </summary>
None.gif
None.gif
public   class  PlanFormatter : ILogFormatter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public PlanFormatter()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public string Format(string MsgStr)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return "{" + MsgStr + "}";
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

再来看一下,在日志记录组件中,它依赖于ILogFormatter

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
InBlock.gif
/// Author:Terrylee
InBlock.gif
InBlock.gif
/// From:http://terrylee.cnblogs.com
InBlock.gif
ExpandedBlockEnd.gif
/// </summary>
None.gif
None.gif
public   class  TextFileLog : ILog
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private string _target;
InBlock.gif
InBlock.gif    
private ILogFormatter _format;
InBlock.gif
InBlock.gif    
public TextFileLog(string target,ILogFormatter format)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this._target = target;
InBlock.gif
InBlock.gif        
this._format = format;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void Write(string MsgStr)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string _MsgStr = _format.Format(MsgStr);
InBlock.gif
InBlock.gif        _MsgStr 
+= _target;
InBlock.gif
InBlock.gif
InBlock.gif        
//Output Message
InBlock.gif

InBlock.gif        Console.WriteLine(
"Output "+_MsgStr);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

那么当我们在容器中加入组件后,它会自动装配TextFormatter还是PlanFormatter呢?以事实说话,先来看一下运行的结果吧:

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
InBlock.gif
/// Author:Terrylee
InBlock.gif
InBlock.gif
/// From:http://terrylee.cnblogs.com
InBlock.gif
ExpandedBlockEnd.gif
/// </summary>
None.gif
None.gif
public   class  App
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//建立容器
InBlock.gif

InBlock.gif        IWindsorContainer container 
= new WindsorContainer( new XmlInterpreter("http://www.cnblogs.com/BasicUsage.xml") );
InBlock.gif
InBlock.gif        
//加入组件
InBlock.gif

InBlock.gif        container.AddComponent(
"txtLog"
InBlock.gif
InBlock.gif            
typeof(ILog), typeof(TextFileLog));
InBlock.gif
InBlock.gif        container.AddComponent(
"planformat"
InBlock.gif
InBlock.gif            
typeof(ILogFormatter), typeof(PlanFormatter));
InBlock.gif
InBlock.gif        container.AddComponent(
"txtformat"
InBlock.gif
InBlock.gif            
typeof(ILogFormatter), typeof(TextFormatter));
InBlock.gif
InBlock.gif        
//获取组件
InBlock.gif
        ILog log = (ILog) container["txtLog"];
InBlock.gif
InBlock.gif        
//使用组件
InBlock.gif
        log.Write("First Castle IOC Demo");
InBlock.gif
InBlock.gif        Console.ReadLine();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

运行程序,结果如下:

ExpandedBlockStart.gif ContractedBlock.gif Output  dot.gif {First Castle IOC Demo} log.txt

也就是说,它自动装配了PlanFormatter,再调整一下加入组件到容器中的顺序:

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
InBlock.gif
/// Author:Terrylee
InBlock.gif
InBlock.gif
/// From:http://terrylee.cnblogs.com
InBlock.gif
ExpandedBlockEnd.gif
/// </summary>
None.gif
None.gif
public   class  App
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// 建立容器
InBlock.gif
        IWindsorContainer container = new WindsorContainer( new XmlInterpreter("http://www.cnblogs.com/BasicUsage.xml") );
InBlock.gif
InBlock.gif        
// 加入组件
InBlock.gif
        container.AddComponent("txtLog"
InBlock.gif
InBlock.gif            
typeof(ILog), typeof(TextFileLog));
InBlock.gif
InBlock.gif
InBlock.gif        
// 注意这里调整了顺序
InBlock.gif
        container.AddComponent("txtformat"
InBlock.gif
InBlock.gif            
typeof(ILogFormatter), typeof(TextFormatter));
InBlock.gif
InBlock.gif        container.AddComponent(
"planformat"
InBlock.gif
InBlock.gif            
typeof(ILogFormatter), typeof(PlanFormatter));
InBlock.gif
InBlock.gif        
// 获取组件
InBlock.gif
        ILog log = (ILog) container["txtLog"];
InBlock.gif
InBlock.gif        
// 使用组件
InBlock.gif
        log.Write("First Castle IOC Demo");
InBlock.gif
InBlock.gif        Console.ReadLine();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

再次运行,出现了下面的结果:

None.gif Output [First Castle IOC Demo]log.txt

这次自动装配的是TextFormatter。由这两次的结果我们可以得出:

如果有多个类(组件)实现同一个接口(服务),容器会自动选择最先加入到容器中的组件来装配。对于这样的结果,其实我们并不感觉到意外,每次注册组件时,容器都会检测它的依赖性,当加入第一个ILogFormatter的组件时,容器检测到TextFileLog已经满足了它的依赖性,所以它不会再去装配第二个。


相关参考:

Castle IOC容器快速入门

Castle IOC容器内幕故事(上)

Castle IOC容器内幕故事(下)

转载于:https://www.cnblogs.com/Terrylee/archive/2006/05/25/408923.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的纺织品企业财务管理系统,源码+数据库+毕业论文+视频演示 在如今社会上,关于信息上面的处理,没有任何一个企业或者个人会忽视,如何让信息急速传递,并且归档储存查询,采用之前的纸张记录模式已经不符合当前使用要求了。所以,对纺织品企业财务信息管理的提升,也为了对纺织品企业财务信息进行更好的维护,纺织品企业财务管理系统的出现就变得水到渠成不可缺少。通过对纺织品企业财务管理系统的开发,不仅仅可以学以致用,让学到的知识变成成果出现,也强化了知识记忆,扩大了知识储备,是提升自我的一种很好的方法。通过具体的开发,对整个软件开发的过程熟练掌握,不论是前期的设计,还是后续的编码测试,都有了很深刻的认知。 纺织品企业财务管理系统通过MySQL数据库与Spring Boot框架进行开发,纺织品企业财务管理系统能够实现对财务人员,员工,收费信息,支出信息,薪资信息,留言信息,报销信息等信息的管理。 通过纺织品企业财务管理系统对相关信息的处理,让信息处理变的更加的系统,更加的规范,这是一个必然的结果。已经处理好的信息,不管是用来查找,还是分析,在效率上都会成倍的提高,让计算机变得更加符合生产需要,变成人们不可缺少的一种信息处理工具,实现了绿色办公,节省社会资源,为环境保护也做了力所能及的贡献。 关键字:纺织品企业财务管理系统,薪资信息,报销信息;SpringBoot
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值