魅力四射的“小玩意”——PicoContainer

原创 2003年08月20日 11:03:00

Jon Tirsen在他的weblog里说,他看到一个叫PicoContainer的有趣项目,并立刻投身其中。这个自称“IoC(Inversion of Control)type 3”的微容器有什么吸引人的魅力?

PicoContainer / NanoContainer I've recently joined two new projects:
http://www.picocontainer.org
http://www.nanocontainer.org

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?". 

To me this question is like saying "What's the point with aircraft? The only thing they do is fly", but I'll give you an example that might highlight why IoC is nice to have. Let's say you have a component A that uses a component B to do something. In the first iteration of your application both are just POJO's registered in a PicoContainer. Here's the code for A:

public class A
{
  B comp;
  public A(B comp)
  {
    this.comp = comp;
  }

  public String helloWorld()
  {
    return "Hello "+comp.world();
  }  
}
PicoContainer will, using IoC, get a reference to B and hand it to A so that it can run. Let's say that we now change so that B is a Jini service that is located using Jini discovery. The code for A now looks like this:
public class A
{
  B comp;
  public A(B comp)
  {
    this.comp = comp;
  }

  public String helloWorld()
  {
    return "Hello "+comp.world();
  }  
}
Looks familiar, doesn't it? But, Jini isn't all that popular these days so we changed B into a webservice and put some failover on top of it using grid computing. With all of this new high-tech stuff running the show, the code for A now looks like this:
public class A
{
  B comp;
  public A(B comp)
  {
    this.comp = comp;
  }

  public String helloWorld()
  {
    return "Hello "+comp.world();
  }  
}
See my point? The code for the A component has not changed one bit even though the lookup and discovery infrastructure has changed radically over the lifetime of our application. This is what IoC brings to the table, and which is what PicoContainer has implemented in a such an elegant way that you don't have to read a lengthy architectural document to grasp how it works.

That's the point.

(NOTE: For those who want to be picky about the above example and say that changing a service to a remote one and evolve it to use transactions and whatnot which would change the code for A, all I can say is: 1) you're missing the point 2) use AOP) (2003-06-29 09:36:39.0)

Python编写一些小玩意儿(《Python核心编程》第11章)

本人理科学生狗,本科时抱着自己以后绝不会从事计算机相关工作的态度而对编程敬而远之,大一学习C++也是苦不堪言,期末背背书上的代码,勉强及格,大二之后再也没有碰过。 对于我这样一个编程菜鸟来说,Pyt...
  • zuolixiangfisher
  • zuolixiangfisher
  • 2015年07月05日 16:35
  • 202

PicoContainer系列之基本介绍

PicoContainer是非常轻量级的Ioc容器,提供依赖注入和对象生命周期管理的功能,纯粹的小而美的Ioc容器。而Spring是Ioc+,提供如AOP等其他功能,是大而全的框架,不只是Ioc容器。...
  • yangguosb
  • yangguosb
  • 2017年11月16日 11:35
  • 56

一些常用的小玩意之Freemarker

1. 网页静态化 可以使用Freemarker实现网页静态化(真正静态化,不是伪静态)。   1.1. 什么是freemarker FreeMarker是一个用Java语言编写的模板引擎,它基...
  • wang_3600
  • wang_3600
  • 2017年11月17日 19:28
  • 29

颠覆常识?!有趣的小玩意

日经杂志(TRENDY),于2014年9月推出特辑,介绍了许多颠覆大家传统思考逻辑的日常小玩意,引起读者们的巨大回响。今天我们就大家来看看这些厉害的小东西吧,究竟都是些什么呢? NO.1 可计重的行...
  • swpton
  • swpton
  • 2014年08月21日 15:02
  • 385

一些常用的小玩意之solr

1. Solr服务搭建 1.1. Solr的环境 Solr是java开发。 需要安装jdk。 安装环境Linux。 需要安装Tomcat。 1.2. 搭建步骤 第一步:把solr 的压缩...
  • wang_3600
  • wang_3600
  • 2017年11月17日 18:56
  • 21

一个小玩意引起的反思

最近在给公司写一个小玩意,虽说写了有几个月了,但写的实在是烂的惨不忍睹.所以,我觉得这事还是值得反思一下的. 几个月前,接到这个活,然后感觉两三周就能搞完,然后开始连续奋战. 一切按照预期的,两三...
  • tjns8
  • tjns8
  • 2016年07月24日 03:38
  • 177

一些常用的小玩意之FastDFS

1. FastDFS 1、存储空间可扩展。 2、提供一个统一的访问方式。   使用FastDFS,分布式文件系统。存储空间可以横向扩展,可以实现服务器的高可用。支持每个节点有备份机。   1.1. 什...
  • wang_3600
  • wang_3600
  • 2017年11月17日 17:25
  • 32

一些常用的小玩意之ActiveMQ

1. ActiveMQ 1.1. 什么是ActiveMQ ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 ...
  • wang_3600
  • wang_3600
  • 2017年11月17日 19:14
  • 25

一些常用的小玩意之dubbo

在基于soa的架构的项目中,表现层和服务层是不同的工程。所以两个系统之间进行通信是必须的。 如何实现远程通信? 1、Webservice:效率不高基于soap协议。项目中不推荐使用。 2、使用rest...
  • wang_3600
  • wang_3600
  • 2017年11月17日 15:42
  • 32

一些常用的小玩意之redis

1. 什么是Redis? Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日起,Redis...
  • wang_3600
  • wang_3600
  • 2017年11月17日 17:46
  • 30
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:魅力四射的“小玩意”——PicoContainer
举报原因:
原因补充:

(最多只允许输入30个字)