Perfection is what I wanted. Writing code examples for a book is harder than you think; Not only does the code need to be right, it also needs to be short and lightweight. Don’t involve API’s you don’t need, don’t create types if you can do without them, that sort of thing.

So I was nearing the handoff deadline and I was going over my code examples. I corrected some and tried to shorten others. For example, in the Chapter 6 example I saw the following code, which I used to abstract session handling from my view logic*:

public class UserTokenProvider implements Provider<UserToken> {
  @Inject private HttpSession session;
  public UserToken get() {
    synchronized (session) {
      return (UserToken) session.getAttribute(UserToken.KEY);
    }
  }
}

public class WebModule extends AbstractModule {
  protected void configure() {
    bind(UserToken.class).toProvider(UserTokenProvider.class);
  }
}

Surely I wasn’t going to list two classes just for a simple provider, so I quickly decided to get rid of the extra class as follows:

public class WebModule extends AbstractModule {
  protected void configure() {
    bind(UserToken.class).toProvider(new Provider<UserToken>() {
      @Inject private HttpSession session;
      public UserToken get() {
        synchronized (session) {
          return (UserToken)session.getAttribute(UserToken.KEY);
        }
      }
    }); // no scope!
  }
}

Now, months later it struck me. This is wrong! (and arguably even less readable) In the shortened version I am now giving Guice a provider instance instead of a provider type. Using that toProvider overload is comparable to using Guice’s toInstance: it will just reuse that instance for all requests to that Key, disregarding all scopes. And when I say all scopes, I mean all scopes. It also ignores the default “no scope”. Using the default scope, Guice will create an instance each time that Key gets requested. However, if you bypass all scopes with toInstance or the toProvider instance overload, Guice will simply reuse your instance.

In my example I was depending on the fact that the HttpSession injected in the provider was always going to be the right one. In the original example it worked as expected. I gave Guice a provider type, and used the default “no scope” so that a new instance would get created for each incoming HTTP request. Guice would inject the right HttpSession instance depending on the request.

Because the second example uses toProvider with an instance instead of a type, it behaves radically different. Ignoring “no scope”, Guice will just inject the first HttpSession instance it finds and from then on it will leave that provider instance alone (a scope widening injection, if you will). Any subsequent requests, from possibly different sessions, will reuse that instance, leading to session corruption and a significant security risk.

Needless to say, I have fixed my code example and published the update to Apress.

Save yourself from such an embarrassment: Remember that toInstance and the toProvider instance overload ignore all scopes, including “no scope”. Avoid using them: use asEagerSingleton to load instances eagerly. Only use these short cuts to fit code on a slide, or in the case of toInstance, to bind constants with better type safety.

* I am not using Guice’s session scope because the UserToken is used as an authentication token in the session. Using scopes, Guice might end up creating a security token for you when it gets requested, which is obviously not what you’d want.

Guice Book Released!

March 28, 2008

I should really get some sleep, but I can’t wait to get the word out. The book I was working on “Google Guice: Agile Lightweight Dependency Injection Framework” has gone live! It’s available in ebook and print-on-demand formats.

Guice Book

Apart from the title, I think the book reflects the fact that I’m a no-nonsense guy. I’m just like you, interested in new technology and I want to see this Guice thing in action. They did that with dependency injection? Hell yes; let’s see why they did it, and who they killed to get there (just kidding :-)).

So, months later I look on Amazon, and see this:

You can get my humble work together with the best Java book ever. That alone was so worth it :-)

Thanks again to Apress, Bob, Dhanji and the Guice community: I couldn’t have done it without you.

Guice Book

December 13, 2007

I’m writing a book on Google Guice, the notorious DI framework created by Bob Lee. I’ve been active in the Guice community for quite a while now, and I have to say that it’s an honor to get the opportunity to write about one of the most innovative Java frameworks out there. Now that it’s been published on Amazon, I feel like I’ve reached the point of no return. The world knows, so I’d damn well better make sure that I deliver some quality. So far I’m having a blast, so perhaps I’m still in Ted’s phase two. :-)

Guice

On a related note, I enjoyed talking to Bob at Javapolis today. On popular demand he took the advanced route for his Guice talk, which I think was appropriate. Too bad he didn’t have enough time to go through the example though. I was pleased to learn that much of what he talked about already made it into the book, in some form. Which must be a good thing, right? Anyway, one more day at Javapolis, and then back to work. :-)

Wish me luck. I’ll try to make sure I won’t need it. ;-)

Follow

Get every new post delivered to your Inbox.