InfoQ

News

Guice: Fast and Light Dependency Injection Container

Posted by Rob Thornton on Mar 08, 2007 12:30 PM

Community
Java
Topics
Artifacts & Tools
Tags
Guice,
Spring

Guice is a new open-source Dependency Injection framework for Java 5 that is closing in on a 1.0 release. Guice is a very annotation-driven, lightweight framework that provides an alternative to Spring, for a certain set of features.

Guice, which is the depenecy injection engine in XWork, focuses on being small, fast, and requiring minimal code to use. The simple example from the Users’ Guide is:

With Guice, you implement modules. Guice passes a binder to your module, and your module uses the binder to map interfaces to implementations. The following module tells Guice to map Service to ServiceImpl in singleton scope:

public class MyModule implements Module {
 protected void configure(Binder binder) {
  binder.bind(Service.class)
  .to(ServiceImpl.class)
  .in(Scopes.SINGLETON);
  }
}

A module tells Guice what we want to inject. Now, how do we tell Guice where we want it injected? With Guice, you annotate constructors, methods and fields with @Inject.

public class Client {
  private final Service service;

  @Inject
 public Client(Service service) {
  this.service = service;
  }

 public void go() {
  service.go();
  }
}

The @Inject annotation makes it clear to a programmer editing your class which members are injected.For Guice to inject Client, we must either directly ask Guice to create an instance for us, or some other class must have Client injected into it.

Bob Lee, one of the project leads of Guice, has written a comparison piece of Spring and Guice.  In it he notes that Guice and Spring look very different, as Guice is focused on annotations.  Guice can be configured with strings, as in Spring, but they don't encourage it.  Guice requires Java 5 and supports generic types. 

Update:  Guice 1.0 has been released.

14 comments

Reply

Congrats by Gavin King Posted Mar 8, 2007 6:26 PM
struts2 by Matt Giacomini Posted Mar 9, 2007 2:23 AM
Re: struts2 by Bob Lee Posted Mar 9, 2007 12:14 PM
comparison to hivemind + tapestry ioc by Kristian Marinkovic Posted Mar 9, 2007 2:41 AM
Re: comparison to hivemind + tapestry ioc by Jesse Kuhnert Posted Mar 9, 2007 6:33 AM
Re: comparison to hivemind + tapestry ioc by Twice Tshwenyane Posted Mar 9, 2007 8:20 AM
Re: comparison to hivemind + tapestry ioc by Bob Lee Posted Mar 9, 2007 12:21 PM
This framework, or somethign like it, should be in the JDK by Weiqi Gao Posted Mar 9, 2007 9:52 AM
Re: This framework, or somethign like it, should be in the JDK by Bob Lee Posted Mar 9, 2007 12:15 PM
Re: This framework, or somethign like it, should be in the JDK by Brent Baxter Posted May 16, 2007 10:54 AM
I Like What I See by Steve Tekell Posted Mar 10, 2007 2:41 PM
should say by Steve Tekell Posted Mar 10, 2007 2:44 PM
Pulga is so simple by Leandro Cruz Posted Mar 11, 2007 9:18 AM
Guice is cool! Mentawai IOC support is very similar! by Sergio Oliveira Posted Jan 29, 2008 1:24 PM
  1. Back to top

    Congrats

    Mar 8, 2007 6:26 PM by Gavin King

    Congrats on this work, there's some really good ideas there :-)

  2. Back to top

    struts2

    Mar 9, 2007 2:23 AM by Matt Giacomini

    Hey guys, This sounds interesting. Any links to where I can find any Guice+Struts2 integration docs/examples? Thanks, ~Matt

  3. Back to top

    comparison to hivemind + tapestry ioc

    Mar 9, 2007 2:41 AM by Kristian Marinkovic

    i don't want to start a flame war, but how does Guice compare to Hivemind 2 (in beta) and Tapestry IOC (part of Tapestry 5). Both are fast and rely heavily on annotations too. Tapestry IOC for example is much more "lightweight" as it does not require to implement an interface for a module at all. Also Tapestry IOC configurations (returning new services) are much more natural to plain old java code :) g, kris

  4. Back to top

    Re: comparison to hivemind + tapestry ioc

    Mar 9, 2007 6:33 AM by Jesse Kuhnert

    I think that there is enough room in this space still to have different implementations running around. It'll keep everything nice & honest in what is still arguably a fairly new programming paradigm for java. I for one am glad to see multiple versions that we can all learn from and evolve with. Er something like that anyways ;) Besides, we probably don't want to mess with Bob too much.heh...

  5. Back to top

    Re: comparison to hivemind + tapestry ioc

    Mar 9, 2007 8:20 AM by Twice Tshwenyane

    +1 As much as i like Spring, i don't think it's good for innovation if there is no competition. So hopefully there will be 1 or 2 things for Spring to learn or give them new ideas. Apart from that i think Spring is still very much useable and easy especially with tools like SpringIDE.

  6. I have always felt that an DI container should be included in the JDK. I have thought about suggesting that Spring's DI container be "standardized" but feared that such a suggestion would be perceived as a sabotage against Interface21. On a very practical level, I would feel a lot better to do an

    import java.lang.inject.Inject;
    than
    import com.google.inject.Inject;
    in my components. On a reuse level, this could eliminate the duplications in some of the half serious dependency injection frameworks in many other frameworks.

  7. Back to top

    Re: struts2

    Mar 9, 2007 12:14 PM by Bob Lee

    There's a small example Struts 2 application in the repos and we talk about it in the user's guide.

  8. Keep an eye on JSR 299. :)

  9. Back to top

    Re: comparison to hivemind + tapestry ioc

    Mar 9, 2007 12:21 PM by Bob Lee

    Doesn't Hivemind use string identifiers? Also, "module" is the Guice equivalent of a configuration file. Guice does not require interfaces. Regarding performance, I have a small benchmark here. If you submit a Hivemind implementation, I'll happily include it.

  10. Back to top

    I Like What I See

    Mar 10, 2007 2:41 PM by Steve Tekell

    I think the same things in Spring as you and Guice look like a great alternative, well for DI anyway. Sometimes I think Spring is the new Struts in it's domain. It has similar timing and popularity, and as time progesses it sure seams like there must be something much, much better. I'll be curious to see if Guice will be able to foster a community and the like to make it a viable long term alternative for rest of us. Congrats on 1.0! looking forward to many more, Steve

  11. Back to top

    should say

    Mar 10, 2007 2:44 PM by Steve Tekell

    I (think that I) dislike the same things in Spring....

  12. Back to top

    Pulga is so simple

    Mar 11, 2007 9:18 AM by Leandro Cruz

    I have implemented a small avalon container called pulga (only 20k, small set of dependencies). Pulga works like an object factory (like pico), using annotations (@Dependency) and conventions. Here is an example: //Create a component public class MyComponentImpl implements MyComponent {

    @Dependency private SomeOtherComponent ref;
    } public class MyTest extends TestCase {
    @Dependency private MyComponent ref; public void testComponent() { assertTrue(ref instanceof MyComponentImpl); }
    } More information at: http://black-beans.com.br:8088/xingu

  13. JSR 299 looks interesting, and I for one would welcome the idea of a managed "web bean" for data driven web apps. What I fear based on the public material available on the JCP web site is that this will be too closely bound to JSF. Can we be sure that any implementation of JSR 299 does not require the use of JSF on the web tier?

  14. Back to top

    Guice is cool! Mentawai IOC support is very similar!

    Jan 29, 2008 1:24 PM by Sergio Oliveira

    I like the programmatic approach used by Guice to configure modules. We have been using programmatic configuration since the start in Mentawai. Check how you can do IoC and Auto-wiring with Mentawai very easily, with no need for an external IOC framework. You are always free to use a external framework, but check this first: http://recipes.mentaframework.org/posts/list/8.page http://recipes.mentaframework.org/posts/list/14.page

Exclusive Content

Using Ruby Fibers for Async I/O: NeverBlock and Revactor

Ruby 1.9's Fibers and non-blocking I/O are getting more attention - we talked to Mohammad A. Ali of the NeverBlock project and Tony Arcieri of the Revactor project.

Agile and Beyond - The Power of Aspirational Teams

Tim Mackinnon talks about the aspirations behind the Agile principles and practices, the desire to become efficient, to write quality code which does not end up being thrown away.

Concurrency: Past and Present

Brian Goetz discusses the difficulties of creating multithreaded programs correctly, incorrect synchronization, race conditions, deadlock, STM, concurrency, alternatives to threads, Erlang, Scala.

ActionScript 3 for Java Programmers

Often the hardest part of changing technologies is language syntax differences. This new article provides Java developers with a transition guide to Actionscript which forms the foundation of Flex.

Neal Ford On Programming Languages and Platforms

Neal Ford talks about having multiple languages running on one of the two major platforms: Java and .NET. He also presents the advantages offered by Ruby compared to static languages like Java or C#.

Future Directions for Agile

David Anderson talks about the history of Agile, the current status of it and his vision for the future. The role of Agile consists in finding ways to implement its principles.

Nick Sieger on JRuby

Nick Sieger talks about the future of JRuby, Java Integration, and his work on JEE deployment tools for Ruby on Rails like Warbler.

Rustan Leino and Mike Barnett on Spec#

Rustan Leino and Mike Barnett of Microsoft Research discuss the technology in Spec# and its futures.