魅力四射的“小玩意”——PicoContainer
Jon Tirsen在他的weblog里说,他看到一个叫PicoContainer的有趣项目,并立刻投身其中。这个自称“IoC(Inversion of Control)type 3”的微容器有什么吸引人的魅力? PicoContainer / NanoContainer I've recently joined two new projects: The founders of the project are Paul (AltRMI,Enterprise Object Broker) and Aslak (XDoclet,MiddleGen). Actually they pair-programmed most of it at Paul's place and a lot of beer was involved. The end result: a neat,simplistic and wonderfully TDDed piece of work. Joe (SiteMesh,QDox),my unit-testing guru,is also in on it. It's basically an Inversion-of-Control-container/framework/micro-kernel. Pico will be the simplistic micro-kernel and Nano will be a bunch of containers serving different purposes (most built on top of Pico). I'm not an IoC-expert by any means,and,well,I didn't know much about it before chatting with Paul and Aslak. The cool (and quite controversial) thing is that Pico (at least by default) implements style 3 IoC,which means constructors are used to define dependencies. Smart! I will implement some Nanning support in Nano so that aspects can define dependencies on services and the container will resolve them properly,the aspects will also be able to aspectify the components transparently. The details are far from finalized,just a bunch of semi-digested ideas. I'll give you a couple of use-cases though. An aspect implementing transparent persistence with Prevayler could retrieve it's Prevayler-instance just by declaring it in its constructor: public class PrevaylerAspect { public PrevaylerAspect(Prevayler prevayler) { /* ... */ } /* ... */ } A declarative transaction aspect could declare it's dependency on a TransactionManager by: public class TransactionAspect { public TransactionAspect(TransactionManager transactionManager) { /* ... */ } /* ... */ } Put these aspects along with their services in a container,Pico does it's work and all components are properly assembled: PicoContainer pico = new HierarchicalPicoContainer.Default(); pico.registerComponent(RemoteTransactionManagerImpl.class); pico.registerComponent(PicoPrevayler.class); pico.registerComponent(PrevaylerAspect.class); pico.registerComponent(TransactionAspect.class); pico.start(); Neat and simple. No XML,no runtime attributes,no fuss. Another great thing is that it's brilliant to mock-test the things. Say you want to mock-test your TransactionAspect to see that it actually demarcates it's transactions properly: MockTransactionManager mockTransactionManager = new MockTransactionManager(); // ...set up expectations and so forth... TransactionAspect transactionAspect = new TransactionAspect(mockTransactionManager); AspectSystem aspectSystem = new AspectSystem(); aspectSystem.addAspect(transactionAspect); TestObject testObject = (TestObject) aspectSystem.newInstance(TestObject.class); // ...run your tests on your testObject... mockTransactionManager.verify(); Check it,you'll like it! (2003-06-20 11:25:47.0) 无独有偶,我的另一位偶像Rickard Oberg也看上了这个项目。他在自己的weblog上介绍了PicoContainer和IOC,还为这个跟人吵起来了。这个小玩意的价值在哪里?我很信赖Oberg的见地。 Why IoC? From the comments on the PicoContainer entry it seems like people are not quite getting what's so great about IoC (Inversion of Control). "If all it does is lookup,what's the point?".
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 数组 – Scala:Condense Array [(String,Array [Double])]
- [leetcode]71. Simplify Path简化路径
- gnu-screen – 如何在屏幕会话中使用Vim的增量(Control-a)?
- CXF学习笔记(1)-HelloWorld!-发布webservice
- scala中的val如何与java中的const不同?
- WebService介绍
- scala – Shapeless在测试中没有发现implicits,但可以在REP
- Scala类型:A类不等于T所在的T:类型T = A.
- 解决bootstrap-select 动态加载数据不显示的问题
- angularjs – transcluded组件无法访问其父级所需的控制器