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.
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:
- Go to http://code.google.com/p/google-guice.
- Click the Downloads tab.
- Download the file named guice-1.0.zip.
- 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.