What is a Dependency Injection? Why do we need it?
Last time I wrote about design patterns on how we can create objects in different ways. Here is another way of creating an object, which is called Dependency Injection. Simply put, dependency is being injected from outside (container). There are 2 of the most popular ways to inject objects. One is to use Spring Framework, the other would be Guice created by Google. Let’s talk about these 2 and how they compare to traditional object creation.
Spring Framework (aka Spring MVC)
I will not go through all of them above. I will explain the Injection via Constructor method. First, you need to load maven dependencies, you may not need the all of these dependencies but they are here more for reference purpose:
We make a simple class with Constructor:
And then we need beans applicationContext.xml:
After you have them ready, prepare your bean xml.
Then finally here is the main class which loads the object:
Now you will be able to get to fileManager and get the directory via Spring DI. As you can see, A Spring bean is basically an object managed by Spring. More specifically, it is an object that is instantiated, configured and otherwise managed by a Spring Framework container. Spring is a POJO-driven although it doesn’t necessarily have to be. That means it is using traditional style to create objects. No strings attached to complicate things.
Google Guice (pronounced as Juice)
Guice is a lightweight dependency injection framework for Java 6 and above. This framework which is released by Google is under the Apache License, meaning you can use it freely as long as you preserve the copyright notice and disclaimers. The main difference with Guice Framework compared to Spring Framework is that Guice uses Annotations. Annotations in Java are first introduced in Java 5, but it was integrated into the Javac compiler in version 1.6 which is probably why Guice is supported for Java 6 and above. There are ways to bind with Annotations in Spring as well. Honestly, I do not see much differences between Spring and Guice. I actually did not like Spring for XML-Based configuration. However, with the newer version of Spring, there are ways around Spring where you can avoid XML definition and be similar to how Guice does its injection.
Here are some useful annotations in Guice:
@Inject - It tells where the injection is happening, you can place on constructors, methods or even fields
@Qualifier - It defines the identity of objects. It has close relationship to @Named annotation
@Named - It is used with @Inject annotation. You can use @Qualifier to name object and use @Named to use it.
@Scope - It tells life span of injecting object. If not defined, will create a new object each time. If defined, thread-safe becomes important.
@Singleton - Similar to @Scope. Basic to DI Framework.
First, just like Spring, you need the dependencies satisfied provided by Google.
What’s important above is that it binds Car.class via AbstractModule which is provided by Google. Let’s test it out.
As you can see, you can be flexible as you wish to create your object. Full examples discussed here are illustrated here.
Share this post
I am a passionate software developer working in Downtown, Vancouver. I strongly believe in art of algorithms and together with it to write clean and efficient software to build awesome products. If you would like to connect with me, choose one from below options :) You can also send me an email at