Over a million developers have joined DZone.

Butterfly Container 2.2.0

DZone's Guide to

Butterfly Container 2.2.0

· ·
Free Resource

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:

http://jenkov.com      (announcement)

http://butterfly.jenkov.com     (home page)


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}