The 5 Minute Guice Primer

I’ve just finished getting up to speed with Google’s Guice 2.0 and have integrated it into two of my projects. It’s been a very comfortable experience and I could see myself Guicifying most of my Java projects from now on.

To summarise my experiences – Guice is a Dependancy Injection and Aspect Oriented Programming framework for Java. This is the turf that’s normally owned by Spring of course.

So how is Guice different to Spring?

  • No XML!
  • Narrower scope: Guice only does DI and AOP, unlike Spring which covers a wide gamut of requirements.
  • Heavy and mandatory use of annotations.
  • …and in my opinion, a more refined and elegant design.

I think one of the main reasons I like Guice so much is the absence of XML. I really hate XML and Guice’s “Module” concept is a great replacement for text configuration. I would be interested to know how often in practice that Spring’s XML configuration is changed in situations where a rebuild of the application is impossible. The argument for that sort of configuration has always seemed like a straw man to me. As a bonus of the Guice Module approach, you get type checking and compilation of your configuration. [It would be interesting to see if someone does implement an XML configurator for Guice though - I'd say it's only a matter of time].

How to get up to speed real fast?

  1. If you’re not familiar with DI and/or AOP then do some reading. A framework can only solve a problem that you know you have.
  2. Watch the recent Google IO tech session for the 60 minute fast-track introduction.
  3. The Guice wiki is helpful, but I found the external links and discussion group to be more helpful.
  4. Write something! It’s a dead easy framework to get your head around, and the fastest way to solidify your knowledge is to do.

Related posts:

  1. AOP, Annotation-based caching solution for Guice projects…
  2. Clustering Guice Java Web Applications
  3. Using Bottom-Up Iterative Object/Database Layer With hbm2java and warp-persist
  4. 5 Minute Guide to Clustering – Java Web Apps in Tomcat
  5. ResourceTransformFilter – DataUris, LessCSS, Javascript and CSS Compression Made Easy!

This entry was posted in General, Java, Software Engineering and tagged Google, guice, ioc, Java, open-source, tips. Bookmark the permalink.

2 Responses to The 5 Minute Guice Primer

  1. Pingback: The 5 Minute Guice Primer « The Pragmatic Coder BEC Development Blog

  2. Adam Smith says:

    I have yet to check out Guice (I plan to), but as far as Spring goes, I have to announce that the emperor has no clothes! No one wants to say the Spring Bandwagon is just a cobbled together mess. I just went through extended training on Spring, and on Day One, I was a fan. By the last day, I was saying no way in hell am I using it! It adds huge complexity in order to simplify. What?! It splits code into Java and XML (or annotations), so it’s hard to follow. It’s a beast to debug. Almost no compile-time checking. Man, it stinks, and no one wants to say it.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>