I'm having trouble understanding what the current "best practice" is
around injecting and using ServletContext in guice-servlet.

ServletModule binds a Provider of ServletContext that is per-injector,
in order to try to eliminate the problem that is a static
ServletContext parameter in GuiceFilter. However, this Provider is
only initialized by GuiceServletContextListener, and not until after
the injector is created, which is somewhat counter to normal Provider
semantics. This appears to create two problems:

1. ServletContext can't be provided to eager singletons without
falling back on the GuiceFilter static ServletContext, which generates
an annoying warning.

2. The docs state "Note that it is not necessary to use a
ServletContextListener to use Guice Servlet, as long as you remember
to install ServletModule when creating your injector." [1] However, if
GuiceServletContextListener isn't used, then the ServletContext
provider is never initialized. This generates the warning even if
you're not injecting a eager singleton!

The current conventional wisdom for solving the eager singleton
problem [2,3] appears to be to subclass ServletContextListener and get
ones own reference to the ServletContext and give it to your Modules
in a constructor. This is possible but requires coupling and
boilerplate I was hoping to avoid.

I don't know of any workaround for not using
GuiceServletContextListener, short of implementing your own context
listener and injecting the ServletContext using a custom binding key,
which seems like a lot of work for a pretty common use case.

Is there another way of solving either of these that I'm not seeing?

Thanks,
Christopher

[1] http://code.google.com/p/google-guice/wiki/ServletModule
[2] https://groups.google.com/d/topic/google-guice/2taOh_2UJh4/discussion
[3] http://code.google.com/p/google-guice/issues/detail?id=603

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to