Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Open Source

Google Guice

Guice (pronounced "Juice") fully embraces modern Java language features and combines simplicity with stunning performance and developer"friendliness. This article is adapted from the book Google Guice: Agile Lightweight Dependency Injection Framework, by Robbie Vanbrabant. Presented courtesy of Apress, all rights reserved.

If you're in the business of creating software, you ultimately want to have maintainable software. You'll certainly agree with me that you spend more time maintaining software than writing software -- and that the maintainability you need doesn't come for free. It requires careful design and a well-defined process for testing and validating the application.

In your professional life, or even as a hobbyist, you've probably picked up the concept of unit testing. Basically, it's about testing little units of source code for validity. Being able to tell with one look at a bar (green or red) whether your code has the right side effects is valuable and will save you time. Unit testing is a no-brainer. Here, unit test examples will use JUnit 4.

I strongly believe that automated testing, like unit testing, is the best way to achieve software maintainability. With the right amount of test coverage, you can rest assured that, when you're making changes to the code, you won't break code somewhere else in the code base. You can simply write your tests, make your change, run the collected set of tests, and feel confident. Poorly designed applications are usually hard to test, which means well tested applications probably aren't too bad. You can write great software without automated testing-you can also win the lottery, but don't count on it.

So there you have it: Unit testing helps achieve maintainability. And what else can help you achieve that? Writing less code, of course! The less code you need to accomplish what you're trying to do, the less code you'll need to maintain. Obviously, you can't just randomly delete blocks of code, but in some cases, code doesn't really mean anything; it's just boilerplate to get you from point A to point B. Wouldn't it be nice if you could get rid of all that noise and focus on the stuff that matters? For lack of a better term, I call this the maintainability mission statement. This is not a complete list, but, among other things, maintainable code needs to be:

  • Easy to test (modular)
  • Meaningful (as little noise as possible)

You probably already see where I'm going, but before we dive into Guice, let me illustrate how to accomplish these goals in a typical situation. When we're done with that, we'll throw Guice into the mix and dance on the ceiling.

Enter Guice

Like most open source software, Guice is freely downloadable on the Internet. However, before you download Guice, make sure that you have the following installed:

Once you have that, you're finally ready to slurp up some Guice:

  1. Go to http://code.google.com/p/google-guice.
  2. Click the Downloads tab.
  3. Download the file named guice-1.0.zip.
  4. Unzip the archive to a directory of your choice.

Inside the archive, you'll find the Guice API documentation and several JAR files. Now, we only need guice-1.0.jar, which holds the core framework. The other ones are either dependencies or extensions.

To follow along with the code examples in this chapter, create a new Java project in your IDE, and add guice-1.0.jar to the class path. Note that because some code listings only show the code relevant to the given section, some examples will not run as they are, but trying out the examples will definitely give you a good feel for how Guice works.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.