Butterfly Container 3.0.0

A dependency injection container

  Add it to your Download Basket!

 Add it to your Watch List!


Rate it!

What's new in Butterfly Container 2.6.0:

  • This version brings easy Map configuration and the ability to assign names to the input streams from which you read container scripts.
  • This name is used when an error is detected in the script, making it easier to find out the file in which the error occurred.
  • It is now also possible to have the ScriptFactoryBuilder close the script input stream for you.
  • This version also fixes a minor bug related to parsing and error messages.
Read full changelog
send us
an update
The Apache License 2.0 
3.4/5 26
Jakob Jenkov
ROOT \ Internet \ HTTP (WWW)
Butterfly Container is a dependency injection container. Butterfly Container is a smaller, yet more flexible and easier to use than Spring, Pico and Guice. Instead of XML Butterfly Container is configured using a simple, flexible, Java-like configuration language.

The Elegant Java Dependency Injection Container.

Butterfly Container was initially developed as a lightweight alternative to Spring. Granted, there was already Pico Container but it has its own rough edges. Later came Guice, but it too has a few quirks.

Though Spring is reasonably straightforward to configure, it is a large framework by now. It takes an effort to know that to include and not to include of its dependencies. In addition its XML configuration files are verbose, tedious to write and not too easy to read.

Simpler, Java-like Configuration

Butterfly Container started out with a simpler XML configuration file format than Springs. This was soon abandoned since it was too inflexible. Instead a Java-like configuration script language was invented, called Butterfly Container Script. As you will see, this script language is a much simpler way to configure a DI container, than the mechanisms used by Spring, Pico and Guice. Here is a simple example:

myBean1 = * com.jenkov.MyBean(); /* simple instantiation*/
myBean2 = * com.jenkov.MyBean(myBean1); /* constructor injection*/
myBean3 = * com.jenkov.MyBean(myBean2)
.setExtraBean(myBean1); /* constructor + setter injection */

Doesn't that look at lot more like Java code than Springs XML config files? It is quite easy to decipher the factory definitions. The MyBean class is just an example. It could be any Java class you like.

More Flexible Configuration

Along with the configuration script language came a lot of nice, very flexible features.

How about extending an existing factory definition? Here is how simple that is:
myBean1 = * com.jenkov.MyBean(); /* simple instantiation*/
myBean2 = myBean1.setValue("Some Value"); /* factory extending myBean1 */

Extending existing factory definitions makes it possible to get rid of close to redundant factory definitions.

Or what about allowing a factory to take input parameters? Here is how:
myBean1 = * com.jenkov.MyBean(); /*simple instantiation*/
myBean2 = myBean1.setValue($0); /*inject input parameter */
myBean3 = myBean2("Parameter Value"); /*factory call with parameter*/

Can you do this with Spring, Pico or Guice?

Still the Lightest

The new, more flexible and advanced configuration language did not make Butterfly Container heavier (in fact it simplified the internal design). With a less than 70 kb JAR file Butterfly Container is still the lightest Java dependency injection container around.

Yet Complete

Even if Butterfly Container is the lightest Java DI container around, it still supports most, if not all, DI features offered by Spring, Pico and Guice. For instance:

· Constructor Injection
· Method Injection (Static + Instance)
· Factory Injection
· Instance Life Cycle Management
· New / Singleton
· Thread Singletons
· Local, Anonymous Factories

Butterfly Container also has a few unique features:
· Method Chaining on Methods Returning Void
· Adaptation to Custom Factory Interfaces
· Flyweights

Last updated on August 5th, 2009


#dependency injection #injection container #Java library #dependency #injection #container #Java

Add your review!