users@grizzly.java.net

Re: ContextListeners and guice

From: Christopher Piggott <cpiggott_at_gmail.com>
Date: Thu, 11 Nov 2010 09:20:32 -0500

Unfortunately, these issues sometimes fall somewhere between jersey,
guice, and grizzly. The whole "system" works well together but
getting it all integrated in just the way you want isn't quite
seamless. Everything gets better with every release.

> I'm not familiar with guice, so wanted to ask if you see any issues related
> to Grizzly?

I guess the reason I asked here was because it frustrates me a little
to have to declare the context listener's class name (through
addServletListener), then let it get instantiated by somebody else. I
don't care for that pattern...I would like control of creating all the
objects myself, either explicitly or through injection. I was trying
to ask "is there another way?"

> ContextListener is regular class, which would be instantiated by calling
> Class.newInstance(), so, theoretically guice annotations should work for it
> the same way as it works for regular POJOs.

What I had forgotten when I asked the question is that you actually
implement a GuiceContextListener (extends ContextListener). This
context listener has a getInjector() method in addition to the usual
contextInitialized() and contextDestroyed().

What I settled on was to write my own class extends
GuiceContextListener; it has a constructor that creates the injector
and stores it in an instance variable; getInjector returns that
injector when asked; and my contextInitialized can therefore know
about the injector and create the items it needs from there. It seems
to work... still not thrilled that it's a beautiful pattern, though.