A Simple Example: Google Guice Inversion of Control (IoC)

If you want to get started with Google Guice, this is the place to start. The previous tutorial demonstrated how to get a simple environment configured, while this one shows you how to do some very simple Inversion of Control.

A Pretty Darned Simple IoC Example with Google Guice

Watch this Google Guice Tutorial as a Video CBT

Tutorial 1: Getting Started with Google Guice: Environment Setup and Configuration
Tutorial 2 (This One): The Simplest IoC Example You've Ever Seen, with Google Guice

In TheServerSide tutorial on basic Inversion of Control with Spring, we took a little class named the GameSummary and had the mighty Spring Container spit out instances to us. The GameSummary class just represented the results of a Rock-Paper-Scissors game, so it has simple String properties such as clientChoice, serverChoice, result and date to represent the time the game was played. Throw in the requisite setters and getters, along with a little toString method, and you feel like you’re dealing with a few klocs of code. 

For the sake of brevity, I’ve taken out the setters and getters for now, and a couple of arrays used in the Spring article have been stripped out, as they’re not really needed right now. Really, all we want is just a simple class, so here is what the GameSummary class we're going to use in this example looks like (updated with a new package statement that makes sure it’s part of the guice folder structure):


package com.mcnz.guice;

public class GameSummary {

  private String clientChoice, serverChoice, result;
  private java.util.Date date = null;

   public String toString() {
       return clientChoice +
             ":" + serverChoice +
                   ":" + result + ":" + date;


Getting Guice to Do IoC

And how do we get Google Guice to inject this cute little POJO into our application? Well, it’s simple. We just ask the Guice injector to get us an instance:

package com.mcnz.guice;
import com.google.inject.*;

public class GumRunner {

  public static void main(String args[]){

    Injector injector = Guice.createInjector();
    GameSummary gs = injector.getInstance(GameSummary.class);


Running the Code

Here's how you can run the code if you're following from the previous tutorial:

C:\_mycode>c:\_jdk1.6\bin\javac -classpath "C:\_guice\*" C:\_mycode\com\mcnz\guice\*.java

C:\_mycode>c:\_jdk1.6\bin\java -classpath "C:\_guice\*";C:\_mycode com.mcnz.guice.GumRunner


And what happens when we compile and run our code now?

Well, when we run our application, we get the following output:



Sure...It's a Very Simple Guice Example

Okay, sure, that’s not the most glamorous output in the world, but what do you expect? We just asked Google Guice to give as an object that had all of its properties set to null. What, did you expect, fireworks and Champaign?

Anyways, the point is, doing inversion of control with Google Guice is pretty darned easy, and if you made it this far, you’ve managed to configure a development environment to run and test Google Guice applications, and you’ve also seen one of the simplest examples of Inversion of Control that you’ll ever see; and that’s a darned good start. But it’s just a start. Stay tuned, because there are more Google Guice tutorials to come.

Watch this Google Guice Tutorial as a Video CBT

Tutorial 1: Getting Started with Google Guice: Environment Setup and Configuration
Tutorial 2: The Simplest IoC Example You've Ever Seen, with Google Guice

Watch the Corresponding Tutorials on Environment Configuration and IoC with Spring 3.

Setting up a Simple Spring 3 Development and Testing Environment: A Tutorial
A Simple Example of Spring 3 IoC withoutXML: A Tutorial

This was last published in May 2010

Dig Deeper on Spring framework

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.