{ by david linsin }

April 22, 2007

Guice DI framework

A couple of weeks ago Google released Guice, a lightweight dependency injection framework. I checked it out and made a small example with Guice.

The example was supposed to be means to get me started using the Guice dependency injection framework. It's dead simple and doesn't contain any reasonable logic. It simply points out how to wire a service using Guice. For further information check out the readme file that comes along with it.

Although there are other DI frameworks than Spring, e.g. PicoContainer or Hivemind, I'm fairly familiar with Spring, so a comparison will come naturally. There is a thorough analysis comparing Guice and Spring available from the Guice development website.

I believe there are advantages and disadvantages for both Guice and Spring. I really like the xml configuration approach using Spring. If you design your application with DI in mind, you'll simply have to add an xml configuration, which wires together your objects. No annotations, no dependencies on any 3rd party stuff. On the other hand no annotations also mean no compile time checks. There is a high possibility of running into problems during refactoring without the compiler helping you out and it gives you a hard time during configuration.

Guice is making heavy use of annotations and generics, which makes it very easy to ensure type safety and correct configuration during runtime. It's not possible e.g. to configure or bind, as it's called in Guice lingo, an interface to an implementation which doesn't actually implement it. In Spring this could possibly happen due to refactoring and not updating the xml configuration.

I consider 3rd party dependencies as somewhat evil. So I like the clean xml configuration approach better. I do admit there are downsides when it comes to type safety or refactoring, but there are tools that can help you with. The Spring IDE e.g. does a great job in pointing out incorrect configurations.



  • mail(dlinsin@gmail.com)
  • jabber(dlinsin@gmail.com)
  • skype(dlinsin)