It has been more than two years since I first acquainted with Spring Dependency Injection technology.
At first, I didn’t understood what it was, but after few weeks I realized what powerful instrument do I have.
From that time I started to love this type of object instantiation and I really don’t want to instantiate objects in some other way.
But today I want to represent you a new framework for bean instantiation - Google Guice.
So, how to try this Google Guice dependency injection framework ?
First of all, open your Eclipse/InteliJIDEA IDE and create maven project: write groupId, artifactId and other things.
After that, go to your pom.xml file and add additional dependency:
Then, you should create interface NotificationService:
We will have multiple implementations of above service to show posibillities of Google Guice
Email service is one of the implementations of NotificationService
One thing that I noticed is that Google Guice has it’s own @Singleton annotation - from JSR330.
Google Guice 3.0 added the support for JSR-330 and we can use annotations from com.google.inject or javax.inject package.
Next, we will create second implementation of NotificationService - FacebookService:
Since we are implementing dependency injection in our application, we won’t initialize the service class in application. Google Guice support both setter-based and constructor-based dependency injection. Our application class that consumes the service looks like below.
Or, if you want constructor-based instantiation
Binding Service implementation
Obviously google guice will not know which service to use, we have to configure it by extending AbstractModule abstract class and provide implementation for configure() method.
As you can see that we can bind any of the implementation to service class. For example, if we want to change to EmailService we would just need to change the bindings.
Our setup is ready, let’s see how to use it with a simple java class.
The implementation is very easy to understand. We need to create Injector object using Guice class createInjector() method where we pass our injector class implementation object. Then we use injector to initialize our consumer class. If we run above class, it will produce following output.
Message sent to Facebook user [email protected] with message=Hi, Ivan
Use of Google Guice for implementing dependency injection in application is very easy and it does it beautifully. It’s used in Google APIs so we can assume that it’s highly tested and reliable code. Download the project from above and play around with it to learn more.