Tutorial:

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

By Jason Tee and Cameron McKenzie

TheServerSide.com

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);
    System.out.println(gs.toString());

 }
}


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:

null:null:null:null

 

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

28 May 2010