InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Google Guice 2.0: Enhanced Capabilities, Less Boilerplate

Posted by Ryan Slobojan on Jun 03, 2009

Sections
Development,
Architecture & Design
Topics
Java ,
Programming
Tags
Dependency Injection ,
OSGi ,
AOP ,
JSR 330 ,
Guice

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.

No comments

Watch Thread Reply

Educational Content

Jesper Boeg on Priming Kanban

In this interview, Jesper Boeg, author of the new InfoQ book – Priming Kanban, discusses the keys to using Kanban effectively, and how to get started if you are currently using other approaches.

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.