Using Guice for Dependency Injection

Prerequisite

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

Introduction

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 Vehicle.java 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 Bike.java 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 Car.java hosted with ❤ by GitHub

Module containing guice bindings.

import com.google.inject.AbstractModule;
public class VehicleShoppingModule extends AbstractModule {
@Override
protected void configure() {
bind(Vehicle.class).to(Bike.class);
}
}

Notice Vehicle class is bound to Bike implementation in module.

Now lets create class VehicleShopping with vehicle as dependency.

import com.google.inject.Inject;
public class VehicleShopping {
private Vehicle vehicle;
@Inject
public VehicleShopping(Vehicle vehicle) {
this.vehicle = vehicle;
}
public void testDrive() {
vehicle.drive();
}
}

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.

import com.google.inject.Guice;
import com.google.inject.Injector;
public class Main {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new VehicleShoppingModule());
VehicleShopping vehicleShopping = injector.getInstance(VehicleShopping.class);
vehicleShopping.testDrive();
}
}
view raw Main.java 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.

Reference:

Google Guice

Dependency Injection – NPE

Clone the project from github

Github Link of the project

If you liked this article and would like one such blog to land in your inbox every week, consider subscribing to our newsletter: https://skillcaptain.substack.com

Leave a Reply

Up ↑

%d bloggers like this: