Google Guice is the framework to automate the dependency injection in applications. If you have come across directly here, I would recommend you to check out Dependency Injection Example where we learned the problems with traditional approach of Object creation and implementation benefits of dependency injection.

In last tutorial, we learned how can we implement dependency injection in applications manually. But when number of classes grow in an application, it’s better to look for some framework to automate this task.

Google Guice is one of the leading frameworks whose main work is to provide automatic implementation of dependency injection. We will work on the same example from last post and learn how can we use Google Guice to automate the implementation process for dependency injection.

Google Guice dependencies are available on maven central, so for maven projects you can add below dependency for it.

If you have a simple java application, then you can download the jar file from Google Guice Home Page on Google Code. Note that in this case you will also need to have it’s transitive dependencies in the classpath or else you will get runtime exception.

For my example, I have a maven project whose project structure looks like below image.


Let’s see each of the components one by one.

Service Classes

MessageService interface provides the base contract for the services.

EmailService is one of the implementation of MessageService. Notice that class is annotated with @Singleton annotation. Since service objects will be created through injector classes, this annotation is provided to let them know that the service classes should be singleton objects.

Google Guice 3.0 added the support for JSR-330 and we can use annotations from or javax.inject package.

Let’s say we have another service implementation to send facebook messages.

Consumer Class

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.

Notice that I have commented the code for constructor based injection, this comes handy when your application provides some other features too that doesn’t need service class object.

Also notice the @Injector annotation, this will be used by Google Guice to inject the service implementation class. If you are not familiar with annotations, check out java annotations tutorial.

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.

Client Application

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.

If we change the bindings to EmailService in AppInjector class then it will produce following output.

JUnit Test Cases

Since we want to test MyApplication class, we are not required to create actual service implementation. We can have a simple Mock service implementation class like below.

My JUnit 4 test class looks like below.

Notice that I am binding MockMessageService class to MessageService by having an anonymous class implementation of AbstractModule. This is done in setUp() method that runs before the test methods.

That’s all for Google Guice Example Tutorial. 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.

By admin

Leave a Reply

%d bloggers like this: