IoC Container :: Open Heart

IoC Container 还真别说,我们还就是决定持久层用hibernate了,这样和gigix的方案一致了。 关于IoC的使用,我能想到的是在表现层和持久层中间加入服务层,这个服务层全部被封装成为组件,使用container注册、管理、调用,在表现层决不有一行调用持久层的代码,全部放在服务层中,这样表现层可以选择各种不同的框架,我们有很多对Struts很熟悉的人,当时我也是设计了这样的架构,不过没有container的概念,要想实现诸如缓存、灵活控制事务、认证、支持多种表现层、支持多种持久层等就做不到。如果有了container,我想是不是可以把这些功能全部放在container中来进行,开发的服务组件只由商业逻辑,其它一概不管,全部由container完成。 至于使用那个container,我还在看,有几个选择: PicoContainer 超轻量级,可以随便嵌入到哪里,不过需要自己完成很多代码,来实现我需要的功能,不知道时间是否允许。使用Tape 3 IoC,要用constructor注册,这个也遭到一些非议,这些没有真正使用过,只是感觉,觉得可以省去配置文件,省去设置context,自动找到dependency,不用实现和pico相关的任何接口,方便单元测试(直接new就行啦,不用烦人的mock,呵呵),更多的好处可能仍需观察中。简单是不是一定就是最好的呢?如果够用,我想应该是吧。 Spring Framework 次轻量级,这个框架就复杂些了,有从头到脚的DIY方案,提供了一个IoC的容器,有很好的和持久层继承的功能,特别对hibernate有参考实现了。他的文档也有比较详细地介绍,不过他采用的是Type 1 IOC,使用xml来注册管理组件,想一下tapestry就要那么多配置文件,就够烦了,还要再来,还没有工具,真是比较头大。他也比较了和Avalon、Pico的一些不同,我贴在下边,做个参考。 WebWork 这个还没有特别仔细看,他把容器部分好像也单独抽取出来形成了xWork框架,似乎完成和pico相似的功能,感觉上可能会考虑的周全些,使用了Type 2 IoC,我在以前的blog也介绍过一下这个东西。 ---------------- 附:String vs Avalon vs PicoContainer Spring Bean Container vs Avalon / Fortress Avalon is the oldest IoC container in the field, although still not very prevalent. It is basically just a set of interfaces for components and service managers, with various implementations. Most of the latter are in somewhat unclear states though, the only major ones are Phoenix and Fortress. For wiring up, every Avalon component needs to look up cooperating components via the Avalon ServiceManager, passed to it automatically if it implements the Serviceable interface. This is pretty intrusive, as practically every application component with dependencies has to implement that Avalon interface. Furthermore, the lookup mechanism is based on the service interface. If you want to receive a specific instance, you have to use the ServiceSelector with a specific key - another special mechanism. A difference in focus shows in that Avalon defines quite a lot of optional interfaces that a component can implement, like Startable, Stoppable, etc. Spring solely offers InitializingBean and DisposableBean as of 1.0 M1, mainly because there has not been demand for explicit start/stop cycles yet. Alternatively, one can also specify an "init-method" and/or "destroy-method" in the bean definition, so that a bean does not even have to implement a Spring-specific interface for such resource handling callbacks. Avalon's most important container implementation, Phoenix, targets standalone applications like full servers. Avalon has its advantages there, as it has been designed for start/stop cycles etc. The only stable implementation for embedded use like within a web application is Fortress, which competes with Spring's bean container. Spring focuses on embedded usage as application context, it does not aim to be a foundation for server processes. And of course, Spring's feature set for application development goes far beyond the bean container itself. Finally, an example of Avalon's programming model: If you'd like to create the above generic application context with Avalon, you'd have to subclass BasicDataSource and make it implement Configurable (for the parameters) and Stoppable (for the close call), or wrap it in a respective service adapter. Furthermore, "exampleDataAccessObject" would have to implement Serviceable to look up the DataSource, and use the ServiceSelector mechanism to choose one by some key if there were more than one. Spring Bean Container vs PicoContainer Another lightweight container getting attention recently is the PicoContainer. Its original incentive is to offer a more lightweight and less intrusive model than Avalon. It is built around components that specify dependencies via constructor arguments, instead of having to use some ServiceManager. This infrastructure intends to solve the same problems as Spring's application context concept but has some severe drawbacks: There's no notion of a specific component instance, just one implementation class for any given type. What if one would like to use 2 DataSource instances for 2 databases? How to configure the 2 differently? How to pass a specific one of those to a certain component? This is not possible with PicoContainer: It registers exactly one instance per type. Pico-managed classes are only allowed to have one single constructor, with all objects that the class depends on as arguments. This does not allow for convenient reuse of the class. Using existing classes is hard with PicoContainer: You have to subclass and write a single magic constructor there. Spring has the ability to work with any existing JavaBean. Inheritance is painful and error-prone when constructor chaining is required. Spring can use final bean properties to avoid errors. A constructor (or any method) with 10 arguments is a code smell and suggests refactoring into an initializer object. Spring can easily support this, via a bean reference. Unit tests concern one method of a service object. Why require providing all params to a constructor if you're really interested in the DataSource property etc? You only need to set the properties you require in Spring. JavaBeans are an elegant part of core Java: Why not use them? PicoContainer objects will need to follow a unique constructor pattern. JavaBean properties can be defined on interfaces if appropriate, or concealed if not. Why build constructors (an implementation issue) into an OO framework? Constructors are less obvious in IDEs than bean properties. It's trivial to see the properties on any object. In the future, Spring may offer dynamic reconfiguration of contexts, if beans can handle it by updating individual changed properties. With constructors alone this would mean changing the identity of the component. PicoContainer's main advantage is that it does not need instance-specific meta data like XML bean definitions, although it does need some kind of component and parameter registration. As this basic design strategy leads to the above mentioned severe limitations, we consider it questionable. Using very simple XML meta data, Spring offers a significantly more flexible and powerful infrastructure that achieves Pico's goals among other things. An example: To create the above generic application context with PicoContainer, you'd have to subclass BasicDataSource to give it one single constructor that takes all "necessary" parameters (is password or validationQuery necessary, for example?), and you would just be able to register one such instance for the DataSource interface (what if you'd like to access 2 databases?). P.S. on PicoContainer's new bean-style factory [24-07-2003] PicoContainer features a bean-style component factory now, as of 1.0 alpha-2, in response to the first version of this article. This can remedy some of the issues that we've mentioned, although PicoContainer still focuses on the constructor-based approach in all of its documentation. Note that the first and most important issue still applies: There is no notion of a specific instance for a given type. We consider passing a reference to a specific instance a very important feature for wiring up applications, which necessarily involves some kind of configuration like in an XML file (Spring makes this pluggable). PicoContainer sacrifices this to be able to adhere to its configuration-less "Type 3 IoC" dogma. Finally, regarding the argument that constructor-based "Type 3 IoC" cannot miss any dependencies: Why should every component need to receive instances for all possible dependencies and parameters? A bean-description-based approach, aka "Type 2 IoC" but without "enabler" interfaces, allows for optional dependencies without any hassle. We consider such flexibility more important than to avoid missing dependencies at any cost. pawa @ 2003-08-25 10:33 引用Trackback(0) | 编辑 评论 我这两天正在看Rod Johnson那本Expert One-to-One J2EE,这位Johnson先生就是Spring的作者。他认为Avalon和WebWork作为容器都不如他的Spring。 在另一个人的weblog上也看到他说“container应该用于提供service”,但具体的优势在哪里,三言两语好象不太能讲清楚。譬如说,我自己做过一个weblog系统,在持久层和展现层之间也有与展示无关、只与业务相关的服务层(service对象都是POJO,方法都是动词,例如“添加新帖”之类)。如果要支持新的展示方式(例如支持XML-RPC),就再提供一个新的facade就行了。那么,container的优势在哪里呢 gigix ( 我想容器是不是可以有和类似EJB Container一样的作用呢?做一些dirty的工作。一般web框架都有一些特殊的机制来获取传输数据,比如在Struts中的ActionForm,我以前的做法是持久层使用的Bean和ActionForm共用。但这样总是扩展性不够强,如果提供一个Helper类来完成表现层和持久层Bean的转换,感觉上结构怪怪的,这样的工作如果放在Container中运行,对于一般的开发者就可以完全不用做这些工作了。针对不同的框架提供不同的Container。不知道这样的想法是不是合理。:) wangdong ( 今天我也看到了PicoContainer. 但没有例子可以试. 明天接着看了... txie ( 发表评论 用户名: Email: 主页: 最后更新 搬家 IoC Container Type 3 IOC 终于有别人也来看我的blog了 EclipseUml 一些AOP的基本知识 单独建立一个AOP的分类 究竟什么是Inversion of Control (or IoC for short)? 如何构建框架? TheServerSide Symposium 纯粹的技术座谈会

原始快照:web.archive.org