← Back to list

From Manual DI to a Framework (Guice)

Manual Dependency Injection vs Guice

Gaurav Verma · 2025-08-02 13:14 · 0 claps · 2.8 min read paywalled
#google-guice #depe #java-di #guice-di-framework #guice
Open on Medium ↗

From Manual DI to a Framework (Guice)

Manual Dependency Injection vs Guice

Manual Dependency Injection vs Guice

In our previous articles, we explored Constructor, Method, and Field-Level Dependency Injection (DI) using pure Java. We learned that while these approaches give us control, they also require significant manual effort. We have to write too many lines of code to create dependencies and then carefully “inject” them at the right time. For Field-Level Injection, this meant cumbersome reflection code that was prone to errors.

What if we could get away with such manual efforts? Is it possible? The answer is “Yes!”

The Problem: Field-Level Injection in Pure Java

Let’s quickly recap our Field-Level Injection analogy. We have a Traveler object that needs a HotelRoom object. The Traveler doesn't explicitly ask for the room but it trusts a TravelPlanner (our manual DI code) to secretly book and inject the HotelRoom into its itinerary.

Here’s the code we had to write to make that work:

// Our Traveler class with a private field for the hotel room
class Traveler {
    private HotelRoom kashmirHotelRoom;

    public void checkIntoHotel() {
        kashmirHotelRoom.prepareForStay(); 
    }
}

// Our manual 'TravelPlanner' using reflection
public class TravelPlanner {
    public static void main(String[] args) throws Exception {
        Traveler happyTraveler = new Traveler();

        // Tedious reflection code to manually inject the dependency
        Field roomField = Traveler.class.getDeclaredField("kashmirHotelRoom");
        roomField.setAccessible(true);
        roomField.set(happyTraveler, new HotelRoom());

        // Now the Traveler can safely check in
        happyTraveler.checkIntoHotel(); 
    }
}

This works, but imagine doing this for every single dependency in your application! This code is verbose (having too many lines), risky and a nightmare to maintain.

This is where powerful frameworks come in. They take on the responsibility of managing and injecting dependencies, allowing us to focus on our application’s business logic. We will now dive into one such framework: Google Guice (pronounced as “Juice”).

What is Google Guice Guice (pronounced ‘Juice’) is an Java based open-source Dependency Injection framework developed by Google and publicly launched in 2007. It’s core purpose is to be your application’s ‘dependency master’. By using Guice, you can get away from the tedious, manual work of managing object dependencies and instead focus entirely on writing your business logic, creating code that is cleaner, more robust and easier to test.

Maven dependency of Google Guice

<dependency>
    <groupId>com.google.inject</groupId>
    <artifactId>guice</artifactId>
    <version>7.0.0</version>
</dependency>

You can find this dependency and its latest version on Maven Central.

The Solution: Field Injection with Google Guice Guice acts as our intelligent “Travel Planner.” It simplifies this entire process by automating the dependency injection for us. Instead of writing too many lines of reflection code, we simply tell Guice what to inject using a simple annotation.

Let’s see how Guice cleans up our code.

First, we update our Traveler class. We don't need any complex code—we just add a single annotation: @Inject.

import com.google.inject.Inject;

class Traveler {
    // Guice will see this annotation and automatically inject a HotelRoom
    @Inject
    private HotelRoom kashmirHotelRoom;

    public void checkIntoHotel() {
        System.out.println("Traveler is preparing to check into the hotel...");
        kashmirHotelRoom.prepareForStay();
        System.out.println("Traveler has successfully checked in!");
    }
}

Next, our TravelPlanner code becomes incredibly simple. We create a Guice Injector, and it hands us back a fully-wired Traveler object, no manual reflection required!

import com.google.inject.Guice;
import com.google.inject.Injector;

public class TravelPlanner {
    public static void main(String[] args) {
        // Step 1: Create a Guice Injector. It knows how to build our classes.
        Injector injector = Guice.createInjector();

        // Step 2: Ask the Injector for a fully configured Traveler object.
        // Guice finds the @Inject annotation and handles the dependency for us.
        Traveler happyTraveler = injector.getInstance(Traveler.class);

        // Step 3: Use our object. No NullPointerException risk here!
        happyTraveler.checkIntoHotel();
    }
}

Did you notice what just happened? All the tedious reflection code (getDeclaredField, setAccessible, set) is gone! Guice took care of all of it for us. This is the power of a modern DI framework. You declare what you need with @Inject, and the framework magically handles the rest, making your code clean, readable, and much more robust.

[embed]Google Guice Introduction

This is just the beginning of what Guice can do! Get ready to explore its full power in our next article, where we’ll dive into Modules and Bindings to build even more robust and flexible applications. 🚀


메타데이터
post_id
2c7c75259f76
slug
from-manual-di-to-a-framework-guice-2c7c75259f76
url
https://medium.com/@gauravverma.career/from-manual-di-to-a-framework-guice-2c7c75259f76
canonical_url
https://medium.com/@gauravverma.career/from-manual-di-to-a-framework-guice-2c7c75259f76
author_url
https://medium.com/@gauravverma.career
status
ok
fetched_at
2026-06-24 23:31:39