The latest expert opinions, articles, and guides for the Java professional.

Hidden pearls of Dependency Injection by Sven Ruppert

As usual, we’re bringing you a short recap of what the latest Virtual JUG session has been about. Virtual JUG is the online Java User Group, which you can join from anywhere over the world. So there’s absolutely no excuse not to join. Here’s a link to the meetup site where you can register yourself and become a part of the community that brings you the most exciting sessions about Java in the world.

The session we’re talking about today was all about dependency injection. Our speaker Sven Ruppert took us through multiple libraries for DI and explained the differences in their approaches and when you might want to prefer one over another.

A couple of words about the speaker. Sven has been coding Java since 1996. He is the head of R&D department in the company called Reply. In his free time he regularly speaks at Conferences like JavaOne, Jfokus, Devoxx, JavaZone, and many more, as well as contributes to IT periodicals as well as tech portals. He is blogging at

Here’s the session fully available on Youtube:

The session started with the basics of the dependency injection. Sven has explained that the main goal of the dependency injection is to accommodate for the lifecycle of objects and larger components of your application. Indeed, if you don’t bother about any lifecycle states, then you can just create the objects yourself and assign them into the appropriate fields.

However, as soon as your system grows, you will need to take more care of how various components have to be initialized and how they are going to work together. The more powerful your DI library is, the more complex combinations of the lifecycles it can support.

After that we’ve dived into the implementation of a small DI framework that is purely based on Java reflection. Indeed, with the access to reflection you can create the components on the fly and proxy the method calls to the right implementation. However this approach has multiple drawbacks, one of which is performance. That’s why you want a system that is as flexible as the reflection based, but more static and thus faster.

During the session Sven looked at various dependency injection libraries and talked pros and cons of using them.

Here’s a list of the libraries that were discussed:

  • Weld — a reference implementation for the context and dependency injection spec in the Java EE. Available in any application server and capable of supporting good old Java SE applications too.
  • Dagger – the first and probably the most well-known DI library for Android apps. It works well on the JVM as well and made it to our list of the best Android libraries Java developers should know about.
  • Dagger2 – is a rewrite of Dagger by Google. It is is a fully static, compile-time dependency injection framework for both Java and Android. The main goals of Dagger 2 are increased performance both at compile-time and during runtime.
  • HK2 – a lightweight and dynamic dependency injection framework for Java. HK2 is super flexible and customizable for various scopes and lifecycle events.
  • DDI – Sven’s pet project: a more dynamic DI implementation that tries to take the best from the other libraries and provide an easy API for mocking, dynamic contexts and such.

It was a great educational session and if you’re curious about the problems that arise from using the dependency injection incorrectly, you’ll definitely benefit from this session.

Interview with the speaker

After the session, we’ve conducted the usual RebelLabs interview with the Virtual JUG speaker and asked Sven about his favorite open source projects, IDEs and tools that he uses daily.
Do you think the dependency injection is a solved problem? Sven doesn’t think so.

Check out the interview and if you have any more questions ping Sven on Twitter: @svenruppert.

What libraries do you use for DI? Share your experiences in the comments section below or tweet at us: @zeroturnaround.

Read next:

No Responses

No comments yet.

RSS feed for comments on this post.

Leave a comment