Introduction to Google Guice

by Shunmuga Raja
04/08/2007

1) Introduction

Google Guice is a Dependency Injection Framework that can be used by Applications where Relation-ship/Dependency between Business Objects have to be maintained manually in the Application code. Since Guice support Java 5.0, it takes the benefit of Generics and Annotations thereby making the code type-safe. This article provides an overview about the Guice framework with a lot many samples. It then looks into the theories related to Dependency Injection Framework and the advantages of using them in Application. It also explores the various API available in Guice along with the Annotations that simplifies most of the things. The final section presents lots of samples thereby making most of the Guice API to get a much better feel towards the API.

2) Dependency Injection

Since Guice is a Dependency Injection Framework, let us make a clear understanding on Dependency Injection which is gaining more popularity in the recent years and a much needed mechanism to be followed in a typical Application. To name a few, J2EE 5.0, Spring, JBoss Seam are good examples that makes use of Dependency Injection. Now, let us take a simple example to illustrate the need for a Dependency Injection Framework.

Consider the following scenario. A Storage represents some kind of repository for storing any type of data. If someone asks us to model the (simple) Class Diagram for this kind of situation, then we could have come up with a design very similar to the one mentioned below.

Storage.java


interface Storage{

    public void store(String uniqueId, Data data);
    public Data retrieve(String uniqueId);

}

The above interface provides a mechanism to store and retrieve Data through its store() and retrieve() methods. Since Data can be stored in a Database or even a File, concrete implementations for the above interface may look something like the following.

FileStorage.java


class FileStorage implements Storage{

    public void store(String uniqueId, Data data){
        // Store the object in a file using Java Serialization mechanism.
    }

    public Data retrieve(String uniqueId){
        // Code to retrieve the object.
    }

}

The FileStorage implementation class may store and retrieve Data in a file which is present in a hard disk. Following is another implementation for the Storage interface where every information is stored in a Database.

DatabaseStorage.java


class DatabaseStorage implements Storage{

    public void store(String uniqueId, Data data){
        // Open a connection and store the data.
    }

    public Data retrieve(String uniqueId){
        // Get the data from the Database.
    }

}

Now, let us look into a Sample Client Application that makes use of this Client. Following is the Client code snippet which initially makes use of FileStorage implementation and then switches over to DatabaseStorage implementation.

StorageClient.java


public class StorageClient {

    public static void main(String[] args) {

        // Making use of file storage. 
        Storage storage = new FileStorage();
        storage.store("123", new Data());

        // Making use of the database.
        storage = new DatabaseStorage();
        storage.store("456", new Data());
    }
}

Note the code in the Client module carefully. Even though, the interface and the implementation classes enjoys loose coupling, the Client module has to manually create instances to the actual implementation classes. Also the relation-ship between the interface and the implementation classes is maintained directly in the Client code. Since, in most of the cases, during the compilation time itself, the Client Application knows to which implementation classes the corresponding interfaces will bound to, will it be useful if someone takes care of maintaining everything. That's what Google Guice does. It takes creating instances in the form of Services from the Application client code and the Dependency between the Clients to its Services is automatically injected through some easy Configuration Mechanism. Following section will provide a simple example that makes use of Guice Framework.