BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Annotation-Driven Dependency Injection with Google Guice 3.0

Annotation-Driven Dependency Injection with Google Guice 3.0

This item in japanese

Bookmarks

Late last month Google released Guice 3.0 to the development community, and in so doing added several extensions on top of the usual bug fixes and patches. Guice, which was originally introduced in 2006, is a Java framework that implements the dependency injection (DI) design pattern. The motivation behind Guice was to make it easier for programmers to write DI code by reducing the need to write boilerplate factories, as is typical when manually injecting dependencies. Instead of writing factories to handle the injections, a developer simply creates a module that maps the interfaces to their implementations, then uses an annotation (like @Inject) on the constructor. Within the module, the programmer writes something like: bind(myInterface.class).to(myImplementation.class);

Once the module is configured just add an @Inject annotation to the client code. Bootstrapping the injector takes just four lines of code: Simply create an injector that points to the module, then use the injector to getInstance() of that module.

The above example uses constructor injection. But Guice supports other types of injections including method (setter) injection, and field injection. This latter form of injection has been criticized because it creates code that can be more obscure and less easy to maintain. The Guice team discourages the use of filed injection for this reason.

A key feature of Guice is that is type safe in that type checking is performed at compile time and the injection returns the same type that is injected. Scopes are handled declaratively using annotations, so developers can define a scope simply by creating an annotation.

 

New in 3.0

The most notable change to Guice 3.0 is the inclusion of a fully compliant JSR 330 injector. (JSR 330 was finalized in 2009.) In fact, “Guice 3.0 is the reference implementation for JSR 330,” according to Dhanji Prasanna of the Guice development team.

Because there is still a need to write factory code, Guice 3.0 adds assisted injection, which automatically generates factory implementations, alleviating the need to write boilerplate code. There are also new Binder methods and enhancements to injections that allow you to return bindings based either by type, scope, existing bindings or all. The framework also extends the SPI for elements to expose elements introduced by third-party extensions. Additional enhancements include improved OSGi support, numerous servlet extensions, support for singletons, and Struts 2 integration. Other features include:

* Simpler stack traces when AOP is involved
* Duplicate bindings are ignored rather than throwing an exception.
* Repackaged cglib, asm & guava classes are now hidden from IDE auto-imports
* Support for Maven

Guice vs. Spring

As is often mentioned in discussion groups the Spring framework already supports dependency injection. The most often voiced concern is that developers must choose between Guice and Spring. While Guice offers an alternative to Spring’s DI methods, it does not supplant the Spring framework. The truth is that Guice can coexist with Spring, and can be used independently or in conjunction with Spring. For example, Guice supports AOP method interceptors, allowing developers to use Spring’s transaction interceptor. There is also a SpringIntegration() class that makes it possible to bind to Spring beans. What Guice brings to the table is type safety, exception handling, and simplicity by removing both an abstraction layer and the need for XML configuration.

In an InfoQ interview, Spring founder Rod Johnson talks about how the annotation-driven injection (annotation binding) in Spring 2.5 was inspired by Guice, and covers the fundamental differences in Spring 3.0’s @Configuration. There is also an updated comparison with Spring on Google Code.

MiniGuice

Finally it’s worth noting that the recent "Guice 4.1" that was announced in some fora was an April fools joke perpetrated by the Guice team. The announcement talked of a stripped-down "MiniGuice," suitable for very small applications. As Guice team member Sam Berlin put it, "Guice 4.1 is a radical rethinking of Guice, dependency injection, and how java libraries are shipped." Guice 4.1 consists of a single file and can be called from this simple API:

myApp program = MiniGuice.inject(myApp.class, new Module1(), new Module2());*

The Guice 4.1 source file can be found at Google Code.

 

Rate this Article

Adoption
Style

BT