dependency injection

Dependency Injection Options for Java

by on February 17, 2014 8:49 am

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.

Dagger

Beginning with one of the simplest, most lightweight frameworks available, 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 DI

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.

— Shannon Griswold, asktheteam@keyholesoftware.com

  • Share:

4 Responses to “Dependency Injection Options for Java”

  1. […] Google Guice is a lightweight injection framework that alleviates the need for the common Factory Patterns many developers turn to when needing to marshal the creation of objects. RoboGuice is an extension of that framework built specifically for Android to help developers cover typical tasks in Android development. There are also other frameworks available, so check out the post by Keyhole’s Shannon Griswold on some of the alternatives – Dependency Injection Options for Java. […]

  2. As a note, Dagger is a Google/Square joint thing. Dagger2 is nearly early access, and is more feature-filled than Dagger 1, 100% compile-time, faster, and has a cleaner configuration model. It lives at http://github.com/google/dagger – the Square folks have given us the go-ahead for the google fork to be canonical for Dagger 2.

  3. Robert says:

    I find Dagger to go in the right direction to eliminate implicit/hidden stuff, but I find it does not go the extra “really no magic” mile. I think that code generation (annotation processing) qualifies as magic in this regard.

    If you allow a shameless plug, check out https://github.com/vanillasource/jaywire, which is pure Java 8 Code, less than 20K. If you want explicit dependency wiring, maybe that is what you are looking for.

Leave a Reply

Things Twitter is Talking About
  • It feels like primitives could have been left out of the initial implementation of #Java. See why - http://t.co/A8ChCBHXJO
    June 29, 2015 at 4:05 PM
  • Developers in a bounce house! I repeat, developers in a bounce house! We had a blast at our 1st company picnic. Pics: http://t.co/XIqs7ECUst
    June 29, 2015 at 1:40 PM
  • New #SpringBatch tutorial from @jhackett01: Spring Batch – Replacing XML Job Configuration With JavaConfig http://t.co/PmdXnriKQu #java
    June 29, 2015 at 11:46 AM
  • We had such a fun time at the Keyhole company picnic! Pictures to come, including some of our developers in the bounce house. #loveourteam
    June 29, 2015 at 8:41 AM
  • In #JavaScript, how do we harness the power of callbacks without the confusing mess of nested functions? Promises - http://t.co/j1gAJ9hi3D
    June 29, 2015 at 8:40 AM
  • .@zachagardner We are so happy that your family attended! This will definitely need to be repeated every year!
    June 28, 2015 at 8:14 PM
  • Thank you to all on the Keyhole team who came to our first inaugural company picnic! Wonderful food, family and bounce house fun!
    June 28, 2015 at 7:50 PM
  • Debugging is a challenging part of being a programmer. We have a tutorial series to help, with a #JavaScript focus - http://t.co/rfhjJo64P7
    June 27, 2015 at 1:45 PM
  • We love #KCDC15! @PinsightMedia's James Hayes just was drawn as the winner of the @KeyholeSoftware drone giveaway. Stop on by the KHS booth.
    June 26, 2015 at 2:24 PM
  • Congratulations to James Hayes! You have won the Keyhole drone giveaway, come up and get your prize! #KCDC15
    June 26, 2015 at 2:09 PM
  • There are just 15 minutes left to register to win the drone from us at the Keyhole Software booth - come say hi and grab some frogs! #kcdc15
    June 26, 2015 at 1:45 PM
  • RT @duanenewman: So many great attendees and speakers at #KCDC15. Sad that we already are at the last day.
    June 26, 2015 at 1:34 PM
  • And thank you to @jonathanfmills! We just ran out of characters in our tweet. #KCDC15 is/has been such a great experience. Thank you to ALL!
    June 26, 2015 at 12:24 PM
  • Huge thank you to @boontlee, @leebrandt & all the #kcdc15 / @kc_dc conference organizers. So valuable for #KansasCity & such fun to sponsor!
    June 26, 2015 at 10:53 AM
  • RT @sixthpoint: Slides from today's presentation "Building a more responsive design with JSF + Bootstrap 3" http://t.co/R2R6KocrUC #kcdc15
    June 26, 2015 at 10:32 AM
  • #KCDC15: Using Java EE 6 Interceptors to provide Business Rules Validation on Stateless EJB Services is @ 10:10 - http://t.co/vtJU358WZk
    June 26, 2015 at 8:06 AM
  • BIG news from the @KeyholeSoftware #KCDC15 booth: @eastlack just set the new world record... his #GrokOla Frog Flinger just went 73 feet!
    June 25, 2015 at 4:02 PM
  • DYK? There is package for Node.js that allows you to invoke .NET code in-process from a #Nodejs app – Edge.js. Info: http://t.co/fYNjfiF3al
    June 25, 2015 at 3:05 PM
  • Debugging is a challenging! @zachagardner wrote this tutorial series to help you hone your strategies - http://t.co/rfhjJo64P7 #JavaScript
    June 25, 2015 at 2:06 PM
  • Just met impressive young women from @MindSTEMs (robotics) @ #KCDC15. They say the best way to help = IT professionals volunteering time!
    June 25, 2015 at 12:55 PM