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
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.
Updated inversion of control (IoC) and dependency injection tutorials and resources
TheServerSide has been updating its resources in terms of the Spring framework, Java EE contexts and various different approaches to inversion of control (IoC) and dependency injection (DI). Check out these newer resources on the topic:
- What does Inversion of Control mean? IoC explained clearly
- A modern Inversion of Control example in Java using Spring
- Drawbacks to IoC problems and how to solve inversion of control problems
- Spring MVC Java web development with Spring Boot
- Spring IoC vs. Google Guice's inversion of control approach
- Getting started with Google Guice
- Getting started with the Spring IoC container
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.
You should follow Cameron McKenzie on Twitter: @cameronmcnz
Interested in more articles and opinion pieces from Cameron McKenzie? Check these out:
- Continuous integration quickstart: A step-by-step Jenkins tutorial for beginners
- A step-by-step Jenkins tutorial
- Create a Jenkins freestyle project
- Shell scripts and the Jenkins environment variables list
- What's better? Jenkins or Maven?
- Can you answer these 10 Jenkins quiz questions?
- Jenkins Git integration with GitHub
- Jenkins Git environment variables
- Microservices deployment? How to embed Tomcat in an executable JAR
Recommended Books for Learning Spring
Spring in Action ~ Craig Walls
Spring Recipes: A Problem-Solution Approach ~ Gary Mak
Professional Java Development with the Spring Framework ~ Rod Johnson PhD
Pro Java EE Spring Patterns: Best Practices and Design Strategies ~ Dhrubojyoti Kayal