InfoQ

News

Guice and JavaConfig: Two Approaches to IoC through Annotations

Posted by Rob Thornton on Mar 27, 2007

Community
Java
Topics
Artifacts & Tools
Tags
Guice ,
Spring ,
Spring JavaConfig

Now that the dust has settled a bit from the initial release of Guice, the comparisons with Spring IoC and specifically with Spring JavaConfig are available. Guice and JavaConfig offer different approaches on putting IoC configuration into code using Java annotations.

Spring JavaConfig, which is currently at a 1.0 Milestone 1 release, is based on the sam idea as Guice in that moving away from XML configuration and towards annotations is good in that it puts the knowledge of the configuration closer to the code. Tapestry's IoC container is designed along the same lines.

JavaConfig and Guice come from very similar backgrounds. In fact, Bob Lee has written about their history:

I also have a slightly interesting anecdote: in the very early days, Guice looked very much like Spring's JavaConfig, which isn't surprising considering Rod showed me Spring's JavaConfig almost a year ago (and I showed him Guice about 6 months ago). Just like with JavaConfig, you wrote explicit Java code to wire your objects. I originally introduced @Inject as an optional way to double-check that the external code called all your setters as expected. Once we had @Inject, I asked, why do we need to write explicit wiring code at all? Shortly thereafter, we had the Guice you all know today.

Several posts comment on the differences between Guice and JavaConfig. Debasish Ghosh sums it up nicely when he writes:

The main difference between Guice and Spring lies in the philosophy of how they both look at dependencies and configuration. Spring preaches the non-invasive approach and takes a completely externalized view towards object dependencies. In Spring, you can either wire up dependencies using XML or Spring JavaConfig or Groovy-Spring DSL or some other option like using Spring-annotations. But irrespective of the techniques you use, dependencies are always externalized.

Guice, on the other hand, treats configuration as a first class citizen of your application model and allows them right into your domain model code. Guice modules indicate what to inject, while annotations indicate where to inject. You annotate the class itself with the injections (through @Inject annotation). The drawback (if you consider it to be one) is that you have to import com.google.inject.* within your domain model. But it ensures locality of intentions, explicit semantics of insitu injections through metadata programming.

Through numerous comment threads on these posts you find that the philisophical difference is the biggest thing to be aware of when making a choice between these two frameworks. How close to your code do you want your injections to be? With Guice, your code will be tied to the container, which some people will like an some won't. Guice claims to be faster, however it is noted that Spring's IoC container (whether JavaConfig or otherwise) allows for a full range of hooks that Guice does not. Spring also offers much more than IoC, while Guice does not try to solve such large problems.

Besides this two, there is Spring-Annotations too ... by Rodrigo Jardim Posted Mar 27, 2007 2:18 PM
Spring without its power and flexibility? Another PicoContainer? by Ronald Miura Posted Mar 29, 2007 6:45 AM
The title should be changed to "Guice and SPRING JavaConfig: ..." by O Z Posted Apr 3, 2007 11:50 PM
  1. Besides this two, there is Spring-Annotations too ...:
    It has moved to a new home, but still in active development
    Spring-Annotations, it uses the Spring Framework engine, but enables all the configuration to be made using annotations
    for example:


    <bean id="test" class="br.com.test...." auto-wire="byName"/>

    is equivalent to @Bean(name="test") in the class ...

  2. Guice seems like a mix of Spring's "byType" autowiring with a mandatory @Required annotation (which is optional), plus Spring-annotation's automatic classpath scanning ability.

    I mean, you take Spring, take all its power and flexibility away, put one (nice) little new feature in, say it's from Google, and that's it, you have now a Spring killer!

    Whatever...

  3. I was confused and thought JavaConfig was yet another IoC framework...

    Adding "Spring" in front of "JavaConfig" would make it clearer...

Educational Content

Brian Marick on 4 Challenges and 5 Guiding Values of Agile Software Development

Brian Marick takes us through a quick tour of the most important values and challenges to adopting Agile successfully (they aren't the typical challenges and values we hear in the community).

Are You a Software Architect?

The line between development and architecture is tricky. Does it exist at all? Is an ivory tower actually needed? There's a balance in the middle, but how do you move from developer to architect?

Agile – A Way of Life and Pragmatic Use of Authority

The word 'authority' sometimes produces an allergic response in hard-line agilists. Freedom and authority – both are bad if misused and both are good if used in right spirit for a noble cause.

Getting Started with Grails, Second Edition

"Getting Started with Grails" brings you up to speed on this modern web framework. Companies as varied as LinkedIn, Wired, and Taco Bell are all using Grails. Are you ready to get started as well?

Using ITIL V3 as a Foundation for SOA Governance

Those familiar with only ITIL V2 often scoff at the thought that ITIL could serve as a governance framework for SOA. With ITIL V3, the focus of the framework shifted towards service-orientation.

Adrian Colyer on AspectJ, tc Server and dm Server

SpringSource CTO Adrian Colyer discusses AspectJ, SpringSource's dm Server and tc Server products, OSGi and Scrum.

Adam Wiggins on Heroku

Heroku's Adam Wiggins talks about Rails, Background Jobs, Add-Ons, Ruby, and how Heroku manages to work around Ruby's inefficiencies using Erlang and other languages.

SOA as an Architectural Pattern: Best Practices in Software Architecture

For Grady Booch the foundation of a good architecture is patterns, SOA being just one of many patterns. In this Second Life presentation, Booch attempts to bring more clarity on what architecture is.