Using Guice for Dependency Injection


For Injecting dependency using guice, you should first know what is Dependency Injection. Please go through this article to understand Dependency Injection.


Now that we know what is Dependency Injection, we can use it in our code. With dependency injection, objects accept dependencies in their constructors. To construct an object, you need first to build its dependencies. But to build each dependency, you need its dependencies, and so on. So when you build an object, you really need to build an object graph.

Here guice comes into the picture. It can build the object graph for you and all you have to is use those dependencies in your code and need not worry about how and where to get these dependencies.

Guice in Action

Guice controls Dependency Injection by Guice bindings.These bindings are used by guice to map object types to their actual implementations. All the bindings are defined in a module.

Let’s create a small application to better understand this. It is a simple application which tells user which vehicle being used for test drive.

Interface for vehicle

public interface Vehicle {
public void drive();
view raw hosted with ❤ by GitHub

Bike implementation of vehicle

public class Bike implements Vehicle {
public void drive() {
System.out.println("I am in Bike implementation of vehicle");
view raw hosted with ❤ by GitHub

Car implementation of vehicle.

public class Car implements Vehicle {
public void drive() {
System.out.println("I am in Car implementation of vehicle");
view raw hosted with ❤ by GitHub

Module containing guice bindings.

public class VehicleShoppingModule extends AbstractModule {
protected void configure() {

Notice Vehicle class is bound to Bike implementation in module.

Now lets create class VehicleShopping with vehicle as dependency.

public class VehicleShopping {
private Vehicle vehicle;
public VehicleShopping(Vehicle vehicle) {
this.vehicle = vehicle;
public void testDrive() {;

We need to create a guice injector for this dependency injection to work. An injector builds the graphs of objects that make up your application and tracks the dependencies for each type and uses bindings to inject them. Thus, the first step is to create an injector and then use the injector to get the objects.This method is to be called once during application startup.

public class Main {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new VehicleShoppingModule());
VehicleShopping vehicleShopping = injector.getInstance(VehicleShopping.class);
view raw hosted with ❤ by GitHub

Response after running application.

I am in Bike implementation of vehicle

Here VehicleShopping class does not care which implementation is provided and all responsibility to get desired objects and their dependency resolved is handled by Guice.


Google Guice

Dependency Injection – NPE

Clone the project from github

Github Link of the project

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

Up ↑

%d bloggers like this: