« Are you scared yet? | Main | JMock vs EasyMock Smackdown »
Friday
Jan232009

Guice 1.0 vs 2.0

The Guice 2.0 contains lots of neat improvements. It removes lots of unnecessary boilerplate and allows to write more compact Guice modules. Here is an example of how a confusing binding can be refactored into a simple provider method.

Example using Guice 1.0:
@Override
protected void configure() {
bind(new TypeLiteral<List<string>>(){})
  .annotatedWith(TagsToProcess.class)
  .toProvider(TagsProvider.class);
}

The same example using Guice 2.0:
@Provides @TagsToProcess
public List<string> provideTags(@Named("tags") String tags) {
  return ImmutableList.of(StringUtil.splitAndTrim(tags, ","));
}

EmailEmail Article to Friend

Reader Comments (1)

Yeah, I love the way annotations work with provider methods.

January 26, 2009 | Unregistered CommenterPeter

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>