- The examples were too trivial to extend to real applications.
- The more complicated examples were doing really horrid things. (Like a singleton class that keeps the injector available for lazy and even not-so-lazy instantiations. Sorry, but why is this better? That's like taking an orange-overall crook, dressing him in a suit and calling him an honest politician. Uhh... wait... that's not what I mean...)
- Guice does not lend itself well to already badly written code, hence the difficulty to find examples that deals with those situations.
- You have one injector and one injector only. Inject once and throw it away. The only exception I make to this is a cool trick in anonymous inner classes (more on this, later).
- Keep the modules few, and put them in the same package as the program entry point. It is after-all the place where the module is used.
- Do not go too far. You can easily over-guice and this DOES make matters worse.
- Do not force Guice down the throat the people that will make use of your code (i.e. if you are writing a library). There is enough of that in the computer world.
Regardless, in order to get this working another injector needs to be passed to the servlet, violating my first rule. However... I found a workaround. I'll let the code speak for itself. Here goes:
First the main class where everything gets wired up. Simple and self-explanatory:
Secondly, the servlet. This merely redirects traffic to the handler and sets the factory. Note the injection. Also note the absense of an injector:
Thirdly the handler with some test injection (demonstrating that we can inject anything we want). It merely echoes the request:
...and lastly, the module. The magic happens in the getRequestProcessorFactoryFactory provider. Have you ever had the need to write an anonymous local inner class within yet another anonymous local inner class? Well, now you do. I call it the Russion-doll pattern (anti-pattern?) Guice has access to the injector so you can merely inject it here as well:
So, the order of creation. The main class gets an instance of the SimpleServer, which needs an instance of a ServletContextHandler, which needs a RequestProcessorFactoryFactory which needs an injector to inject an instance of a new handler. Basically boils down to lazy instantiation which is set up in the module.