Guice: Fast and Light Dependency Injection Container
- Share
-
- |
Read later
Reading List

A note to our readers: You asked so we have developed a set of features that allow you to reduce the noise: you can get email and web notifications for topics you are interested in. Learn more about our new features.
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.
Rate this Article
- Editor Review
- Chief Editor Action
Hello stranger!
You need to Register an InfoQ account or Login or login to post comments. But there's so much more behind being registered.Get the most out of the InfoQ experience.
Tell us what you think
struts2
by
Matt Giacomini
This sounds interesting. Any links to where I can find any Guice+Struts2 integration docs/examples?
Thanks,
~Matt
comparison to hivemind + tapestry ioc
by
Kristian Marinkovic
g,
kris
Re: comparison to hivemind + tapestry ioc
by
Jesse Kuhnert
Re: comparison to hivemind + tapestry ioc
by
Twice Tshwenyane
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.
This framework, or somethign like it, should be in the JDK
by
Weiqi Gao
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.
Re: struts2
by
Bob Lee
Re: This framework, or somethign like it, should be in the JDK
by
Bob Lee
Re: comparison to hivemind + tapestry ioc
by
Bob Lee
Regarding performance, I have a small benchmark here. If you submit a Hivemind implementation, I'll happily include it.
I Like What I See
by
Steve Tekell
Congrats on 1.0!
looking forward to many more,
Steve
Pulga is so simple
by
Leandro Cruz
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: black-beans.com.br:8088/xingu
Re: This framework, or somethign like it, should be in the JDK
by
Brent Baxter
Guice is cool! Mentawai IOC support is very similar!
by
Sergio Oliveira
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:
recipes.mentaframework.org/posts/list/8.page
recipes.mentaframework.org/posts/list/14.page