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:

One Response 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. […]

Leave a Reply

Things Twitter is Talking About
  • Thank your #Sysadmin - today is System Administrator Appreciation Day. http://t.co/LcvDNa9kPg
    July 25, 2014 at 8:05 AM
  • @rickincanada Thx for your tweet! Shoot us an email at asktheteam@keyholesoftware.com so we can set up a time to talk. Have a good day.
    July 24, 2014 at 3:33 PM
  • Never used JAXB? Check out a simple usage pattern that pairs #JAXB’s data binding capabilities with JPA - http://t.co/Ki9G04HV5e
    July 24, 2014 at 9:53 AM
  • Guess what today is? Tell An Old Joke Day - http://t.co/835ORWMX6N! So, why do programmers always confuse Halloween & Xmas? 31 Oct = 25 Dec
    July 24, 2014 at 8:45 AM
  • MT @midwestio: Posted another #midwestio talk recording to our YouTube channel: @MinaMarkham on modular CSS. Watch: http://t.co/aU3LpfUoi4
    July 24, 2014 at 8:25 AM
  • We just posted pictures from our National Hot Dog Day Lunch Cookout. Check them out - http://t.co/To06plaw1C
    July 23, 2014 at 4:14 PM
  • Good free cheat sheet - #Java Performance Optimization Refcard from @DZone: http://t.co/7vBgsmqy08
    July 23, 2014 at 10:48 AM
  • Did you know today is a holiday? It's National Hot Dog Day! We're gearing up for our team lunch hot dog cookout & can't wait to celebrate.
    July 23, 2014 at 9:43 AM
  • Check out our newest blog: #JAXB – A Newcomer’s Perspective, Part 1 http://t.co/Ki9G04HV5e
    July 22, 2014 at 1:22 PM
  • New post on the Keyhole blog by Mark Adelsberger: #JAXB – A Newcomer’s Perspective, Part 1 http://t.co/Ki9G04HV5e
    July 21, 2014 at 2:27 PM
  • If you're a Java dev, you're likely familiar with Annotations. But have you created your own #Java Annotations? Ex - http://t.co/BgCsYjxZKF
    July 18, 2014 at 12:10 PM
  • RT @gamasutra: Don't Miss: Unconventional Tips for Improving your Programming Skills http://t.co/6TFox7CKHU
    July 16, 2014 at 3:20 PM
  • We're about to send out our free monthly tech newsletter. Dev tips/articles via email. Not on the list yet? Sign up - http://t.co/F8h0NSiicZ
    July 15, 2014 at 11:57 AM
  • Have you ever tried creating your own #Java annotations? See a situation where it was beneficial - http://t.co/BgCsYjxZKF
    July 15, 2014 at 8:36 AM
  • There's a new post on the Keyhole blog by @jhackett01: Creating Your Own #Java Annotations - http://t.co/BgCsYjxZKF
    July 14, 2014 at 1:43 PM
  • We love development! Have you seen our weekly team blog? We show how to be successful with the tech we use. See it - http://t.co/nlRtb1XNQH
    July 12, 2014 at 2:35 PM
  • Rapid appdev has a bad rep, but there are ways to bring development time down the right way. Don't Fear the Rapid - http://t.co/aTPcAKOj0r
    July 11, 2014 at 3:10 PM
  • Automated Testing is great for dev, but does bring a set of challenges (especially for #agile teams). Success tips: http://t.co/1acl1ngO7i
    July 11, 2014 at 12:16 PM
  • This is fantastic - One small step for Google, one giant leap for empowering girls to code: http://t.co/R90V5DBkv1
    July 10, 2014 at 2:52 PM
  • #RabbitMQ: messaging software built on AMQP protocol. Learn relevant concepts & how to avoid common "gotchas" here: http://t.co/ZwMXlhKyX8
    July 10, 2014 at 9:31 AM
Keyhole Software
8900 State Line Road, Suite 455
Leawood, KS 66206
ph: 877-521-7769
© 2014 Keyhole Software, LLC. All rights reserved.