Computers have been my hobby since I was 12. Now I'm a freelance Java developer. Like many other developers I am working on various private projects. Some are open source components (Butterfly Components - DI container, web ui, persistence api, mock test api etc.). Some are the tutorials at Yet others are web projects. I hold a bachelor degree in computer science and a master degree in IT focused on P2P networks. Jakob has posted 35 posts at DZone. You can read more from them at their website. View Full User Profile

Butterfly Container 2.2.0

  • submit to reddit

Butterfly Container is a small dependency injection container like Spring's core, Pico container and Guice. It is currently an 87 KB jar file, but supports most of the important features a DI container needs.

The major difference from Butterfly Container to these other containers is that Butterfly Container allows you to configure the container using a configuration script language that is much simpler than Spring's XML. It looks more like Java, and is thus more intuitive in many cases, once you get the hang of it. It is also more concise and flexible. Here is an example of a configuration:

beanA = * com.jenkov.myapp.BeanA();
beanB = * com.jenkov.myapp.BeanB(beanA).setValue("additional value");
beanC = * beanB.setValue("C value instead").setMoreValues("a", "b", "c");

As you can see Butterfly Container Script is inspired by Java, with a few changes, like method chaining allowed on void methods (void methods are interpreted as returning "this").

You can also configure the container by plugging in plain non-annotation, non-reflection Java factories into the container, in case you need more speed during instantiation, or just don't want to externalize the container configuration.

Butterfly Container 2.2.0 makes it possible to replace factories in the container at runtime. This is a handy feature during unit testing, which makes it possible to override certain configurations while testing a specific component in the container. For instance, replacing real collaborator configurations with mock configurations.

A performance test available from the website shows that Butterfly Container is faster than Guice, and Guice claims to be faster than Spring...

Butterfly Container is part of a small component collection which currently also contains components for mock unit testing. In the future Butterfly Components will also contain components for doing web ui's, and for relational persistence (Mr Persister will be retrofitted for this purpose).

Go check it out here:      (announcement)     (home page)

Published at DZone with permission of its author, Jakob Jenkov.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)