BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Google Guice 2.0: Enhanced Capabilities, Less Boilerplate

Google Guice 2.0: Enhanced Capabilities, Less Boilerplate

This item in japanese

Bookmarks

Guice, a lightweight Java dependency injection framework created by Google, recently released version 2.0. InfoQ spoke with Google Developer Team member Jesse Wilson to learn more about this release and what capabilities it adds to Guice.

Wilson described Guice's philosophy as "writing code with purpose", with the idea that time spent writing the application framework is not time spent writing business functionality. The intent of Guice is to be minimally intrusive, and it uses an annotation-based approach to dependency injection along with Java-based configuration. Once it creates the initial object graph and injects all of the dependencies, it lets the business logic take over and does not need to be thought about any more. In comparison with frameworks such as Spring and Webworx which are full-stack enterprise application development frameworks, Guice is a dependency injection kernel which has integrations into a number of areas.

Major new features included in this release are:

  • Provider methods - These eliminate a lot of the boilerplate code which was necessary in 1.0 when creating custom providers
  • Enhanced module capabilities - New features such as Binding overrides and Private modules allow for better overriding and compartmentalization of module configurations
  • Improved error reporting - Stack traces now no longer have very long 'caused by' chains, and exception throwing is now consistent throughout Guice
  • AOP - Guice is now available without AOP for platforms which do not support it (such as Android), and it also now does AOP in an OSGi-friendly manner
  • Servlet support - Programmatic configuration of servlets and filters with no web.xml is now supported with the ServletModule
  • Introspection - Similar to java.lang.reflect, you can now introspect and alter Guice configuration programatically

A complete feature list is available on the Google Guice wiki, as well as a multi-part detailed walkthrough of the new features in a series of blog articles.

Wilson offered an example of some of the simplification which has occurred with provider methods:

in Guice 1.0, configuring custom business logic to provide an instance was pretty gross:
  bind(Integer.class)
    .annotatedWith(Names.named("lucky"))
    .toProvider(new Provider() {
@Inject @Named("unlucky") int unlucky;
public Integer get() {
return unlucky * 7;
}
}).in(Singleton.class);
Provider methods really tidy this up:
  @Provides @Named("lucky") @Singleton
  int provideLucky(@Named("unlucky") int unlucky) {
    return unlucky * 7;
  }

Wilson also discussed the utilization and community aspects of Guice, saying that Guice is used heavily inside Google on many projects such as AdWords, Blogger and Gmail. There are also other big users of Guice such as LimeWire, and a list of third-party modules for Guice is also available which includes integrations for Java EE, OSGi, JBoss, Hibernate, JUnit and JSF. Wilson gave a lot of praise to the energy and enthusiasm of the Guice community, and also pointed out that the community has created C++, C#, GWT and PHP ports of Guice. When asked about future plans, Wilson indicated that the Guice team would be contributing resources to the JSR 330 Dependency Injection proposal. He also mentioned that several features which did not make it into the 2.0 release, such as a visualization graph for injectors, will be worked upon and added in future Guice releases.

Rate this Article

Adoption
Style

BT