About Keyhole Software

Keyhole is a midwest-based consulting firm with a tight-knit technical team. We work primarily with Java, JavaScript and .NET technologies, specializing in application development. We love the challenge that comes in consulting and blog often regarding some of the technical situations and technologies we face.

Dependency Injection Options for Java

I wanted to take some time to put together a summary of some popular dependency injection (DI) frameworks for Java. This is a high-level overview of what’s available.

First off, what is dependency injection?

“Dependency injection is a software design pattern that allows the removal of hard-coded dependencies and makes it possible to change them, whether at run-time or compile-time.” – Wikipedia

If you’ve ever had a constructor with 12 parameters, you’ve already encountered a compelling reason to use dependency injection.

Most objects require references to other objects to be useful. Dependency injection allows the system to provide those references without you having to manage all of those dependencies at the application level.

This sounds complicated, but it’s really not. Since this is not an in-depth discussion of dependency injection, check out the Wikipedia page for several examples in various languages.

Square Dagger

Beginning with one of the simplest, most lightweight frameworks available, Square Dagger is a tiny library (<100KB) that is useful for programs that must minimize their footprint, as in the case of mobile applications. It lacks many of the features of the larger frameworks, but it makes it up in speed and a neat compile-time validation tool.

Google Guice

Guice is Google’s attempt to create a feature-rich, independent DI facility. It is useful in most applications, especially web development. It provides a lot more features than Dagger, but it is slower and can be complex.

Spring is huge. It’ll do everything for you, even write perl so that the perl can then do your laundry. Of course it has dependency injection.

The upside is that if you are already using Spring, enabling and using DI is very easy and makes for a very well integrated approach.

The downside is that if Spring isn’t your thing or you are trying to add DI to an existing enterprise application, it may be challenging to isolate the small subset of features that you want to use.

Spring DI can be configured either through XML or through annotations. I strongly recommend the annotated approach. It is much easier to traverse in code and can provide significant insights to developers that need to understand what’s going on.

Java EE6 CDI

CDI is the Context and Dependency Injection framework that is included in Java Enterprise Edition. On the surface, it looks very similar to Spring’s annotation-based DI mechanism and, in reality, it is. There are some things that it does differently under the covers, but for the most part it is attempting to solve exactly the same problems for the Java EE crowd.

PicoContainer

I mention this one last because I just came across it while poking around the internet. PicoContainer, like Dagger, is a very small framework that intends to do a few things very well. It doesn’t have the features or complexity of Spring or CDI, and in exchange you get simplicity.

I don’t know much about this one but I am now aware of its existence. A new adventure every day!

Deduplication

This post was published with the permission of one of our software consultants, Shannon Griswold (www.thegrisexplores.com). Original text can be found here.
 

Reference: Dependency Injection Options for Java from our JCG partner Shannon Griswold at the Keyhole Software blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

3 Responses to "Dependency Injection Options for Java"

  1. Michael Russo says:

    Looks like you missed the header for Spring DI, making it look like Guice flows right into Spring.

  2. KB says:

    “Square Dagger” is really just called “Dagger.”

    (For that matter, “Google Guice” is just “Guice”, but in that case at least it really was primarily driven by just the one company.)

  3. Can I suggest a new DIC project? http://fbn.github.io/gimple/
    It’s a Micro (one class) DIC container inspired by PHP PImple.

Leave a Reply


8 − seven =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close