Books I Like

Sunday, October 18, 2009

Google Guice : Part 1

Over the weekend I was going through this video presented at Google IO 2009.
Please watch it if DI (a.k.a Dependency Injection) interests you.

Where is Guice being used in production :-
The video mentions Google uses it for Google Wave, Google Adwords, GMail, Orkut, Google Docs, Youtube...pretty much every new project .

This is how I talked with myself, in case it is gonna help you let us learn together.

Should I care to learn Guice, I already know/use Spring :-
Spring framework is much more than a DI framework.It made DI popular (although there were older DI framework available before it). Spring competes with JEE stack.
So, yeah we can say Spring is a super set of Google Guice. Guice wholly embraces annotations and generics, thereby enabling you to wire together and test objects with measurably less effort. Guice proves you can use annotations instead of error-prone, refactoring-adverse string identifiers.And of course it is much light weight.

Spring uses annotations too :- That is true. The newer versions (probably 2+) focuses more to use annotations. We may discuss the benefits of XML declarations vs annotations in a separate post, as it is a topic in itself .

There are so many annotations for different DI libraries, shouldn't it be standardized :- You are in luck. Check it out JSR330- show me some code :-

I won't boar you with the definition(s) of DI, IOC (search it on google rather in case you are new to this). Goal of this post is to make you run a simple Guice Program.

Download the guice library, and add it in your classpath.

The interfaces and it's implementations are self explanatory :-

public interface IPrinter {
public void print();

public class MyDummyPrinter implements IPrinter {
public void print() {
System.out.println("Article Printed !");

public interface IMailSender {
public void sendMail();

public class MyDummyMailSender implements IMailSender {
public void sendMail() {
System.out.println("Mail Sent !");

public interface ArticleService {
public void publishArticle();

public class Article implements ArticleService{
private IMailSender mailSender;
private IPrinter printer;

Article (IMailSender mailSender, IPrinter printer) {
this.mailSender = mailSender;
this.printer = printer;
public void publishArticle() {

As shown in the above program, we want the mailSender and printer to be injected.
So, note the @inject annotation over the Article Constructor.

Lets inject the dependencies. Well, if you have worked with Spring's XML declarations for defining and injecting the dependencies, then think the next java code to be it's counter part in Guice.

public class MyArticleModule extends AbstractModule {
protected void configure() {

What I like here is the readability (It is like I am coding in Ruby :)). The code clearly shows that IPrinter.class is bound to MyDummyPrinter.class. Similarly, with the other line.

Cool..Now let us launch/bootstrap the Guice. For the Spring folks, think it similar to the code you write to load spring config beans (remember : ApplicationContext ctx = new ClasspathApplicationContext("example.xml")).


public class MyBootStrapInvoker {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new MyArticleModule());
Article article = injector.getInstance(Article.class);

And that's it. You are running Guice !!!

Hopefully, we will dive more in depth in the second post on the same topic.
Please let me know your comments, so the next time I could offer more.

1 comment:

Wim Molenberghs said...

Good introduction about Guice Anshu.

I've also written a tutorial about Guice a while ago

Get at Amazon