本文只是通过一个简单的例子来比较一下两者配置之间的区别。在Castle IOC容器中,提出了自动装配(Auto-Wiring)的概念,即由容器自动管理组件之间的依赖关系,我们无需自己编写XML配置文件来配置组件之间的依赖关系。在Spring.NET中也是支持自动装配的,但是并不推荐使用,它贯穿着一种思想就是一切皆为XML配置,这是两者之间最大的一个区别。
关于自动装配,来自于Spring.NET的支持者认为让容器自动管理,会让我们无法控制组件的依赖关系,如果该为XML配置,可以让我们知道自己在做什么,我们指定了哪些依赖关系,方便进行控制和管理;而来自于Castle IOC的支持者认为如果不让容器自动管理,手工配置会变得非常之复杂,配置文件也会变得非常繁冗,如果系统中的组件非常之多的时候,管理工作会变得很困难。
我们来看一个简单的例子,有这样一个组件MyMainComponent,它依赖于MyComponent1、MyComponent2,并且它在构造函数中还需要接收一个整型的参数。
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
如果用采用Spring.NET,它采用XML进行组件之间的连接,配置文件如下,需要在配置文件中指定每一个对象及其它们之间的依赖,同时在配置文件中区分是构造函数还是其他方法:
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
Castle IOC中同样需要配置文件,但相比之下,就简单了很多:
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
在Castle IOC中的配置并不需要指定组件之间的关联,它会自动通过Windsor来处理;我们只是配置了一个参数i,这个i是MyMainComponent中的构造函数中不存在依赖关系的那个参数。
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这样添加组件后,
WindsorContainer会自动调用MicroKernel中的ConstructorDependenciesModelInspector来处理组件的构造函数依赖。通过上面的这个简单例子比较可以看出,如果我们想要增加一个组件之间的依赖关系或者增加一个组件使用Castle要比使用Spring.NET容易很多,Spring.NET复杂的配置文件会给我们开发带来很来不可预料的错误;Castle根据对象的依赖关系,采用自动装配,不需要配置组件的依赖,另外为了符合构造注入和属性注入,Castle的配置文件并没有像Spring.Net那样区分构造函数还是其他的方法,同时直接使用Parameters,而不是使用构造函数参数之类的区分。