Guice Dependency Injection

Dependency Injection:

The behavior of Dependency Injection is like leave the bag to others. Not initialize the object each time we use it. We initialize it once and inject the dependency (pass as reference) each time. Still confused? Let’s split it into an example.

Say we have a TicketService class as follow

public class TicketService {
    private final Ticket ticket

    public TicketService(Ticket ticket) {
        this.ticket = ticket;
    }

    ...
}

In the ticket class, it do a complex disk and network manipulation

public class Ticket {
    public Ticket() {
        /**
         * Complex disk and network manipulation
         */
    }
}

Ticket is the dependency of TicketService. The way we passing the dependency (Ticket) to TicketService is called dependency injection.

Factory Pattern

In a contrast example, we can use factory to initialize the dependecy in TicketService. Which is called Factory Pattern.

public class TicketService {
    private final Ticket ticket = TicketFactory.getInstance();

    ...
}

Why Guice?

Now think about the scenario we are writing integration tests. TestA() and TestB() are different classes, and both of them need to use TicketService. What we do now? Shall we initialize TicketService seperately in TestA and TestB? It’s a waste of resource.

Above the a simple example. We can also think about ticket has its dependency, so on and so on. Which makes Guice more convenient in implementing Dependency Injection Pattern.

In short, the advantages are:

  1. Save runtime resources (Dependency Injection)
  2. Write less annoying object initialize, make Dependency Injection Easier (Guice)

Dependency Binding

public class ServiceTicketModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(TicketService.class).to(Ticket.class);
    }
}
public class TestA() {
    Injector injector = Guice.createInjector(new ServiceTicketModule());
    ServiceTicket serviceTicket =
injector.getInstance(TicketService.class);
}

Refer the official documents for more information.