Not convinced about Guice, Spring scores again with JavaConfig
March 21, 2007
I’ve been a long time fan of IoC and DI, and have used Spring as my IoC container pretty much exclusively. But I’ve never been a fan of XML-based configuration. So when I first heard about Google Guice I was excited. Over the course of the last couple of weeks I’ve checked out Guice and read a lot of blog posts about it, and in particular about Guice v. Spring. There are a couple of people around saying that Guice is the Spring killer and that Spring has become bloated and closer in nature to that which was despised (i.e. EJB/J2EE). I don’t agree with that sentiment. Guice is no replacement for Spring and does not claim to be. Also, Spring is as lightweight as you want it to be, it is a toolkit and you are free to choose what bits you want from it, or none at all. This debate aside, when I came across Guice I gave it very serious consideration. (There are plenty of tutorials etc. about Guice, so I’m not going to repeat them here.) But it wasn’t long before I found three things I really did not like about it:
- The need to annotate the classes being managed by the IoC container (@Injection etc.).
- The difficulty (and I’ll stand corrected if there is a nice/easy way around this) of injecting different implementations for the same interface depending on the context – something which I need to do all the time.
- The (ridiculous and cumbersome IMHO) need to annotate properties for property injection. Spring is not super fantastic in this regard either, requiring the presence of getters/setters or appropriate constructors, but its still better than the annotation explosion that Guice requires.
So, Spring’s “answer” to Guice is JavaConfig. Wow, the power of competition. Thankyou Google. JavaConfig makes a heck of a lot more sense that XML-based configuration. Craig Walls has an excellent comparison of Spring JavaConfig and Guice. Really, the only point Guice scores against Spring now is speed, and that is not a proper comparison (there is a reason Spring is slower doing injection). More importantly, 99% of the time I don’t see performance in this area being significant when considered in the bigger picture.
I still don’t know enough about Guice to decide definitively where I stand on this one, but for now I’m happily switching to JavaConfig and I’ll continue to watch the space.