Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I can think of a few options.</p> <p>(1) Register your listeners as normal (in web.xml) and retrieve the injector from a servlet context attribute. <code>GuiceServletContextListener</code> put the injector instance in the servlet context after initialization with the attribute name of <code>Injector.class.getName()</code>. I'm not sure this is documented or supported, so you may want to define your own attribute name for the injector and put it there yourself. Just make sure that you account for initialization order of listeners--the injector will not be bound until your GuiceServletContextListener is invoked.</p> <pre><code>class MyListenerExample implement HttpSessionListener { // or whatever listener static final String INJECTOR_NAME = Injector.class.getName(); public void sessionCreated(HttpSessionEvent se) { ServletContext sc = se.getSession().getServletContext(); Injector injector = (Injector)sc.getAttribute(INJECTOR_NAME); // ... } } </code></pre> <p>(2) If you are using Java Servlets API version 3.0+, you can call <code>addListener</code> on the ServletContext. I would probably suggest you do it right when you create your injector, though you can do it wherever. This approach feels a little hacky to me, but should work.</p> <pre><code>public class MyServletConfig extends GuiceServletContextListener { ServletContext servletContext; @Override public void contextInitialized(ServletContextEvent event) { servletContext = event.getServletContext(); // the super call here is where Guice Servlets calls // getInjector (below), binds the ServletContext to the // injector and stores the injector in the ServletContext. super.contextInitialized(event); } @Override protected Injector getInjector() { Injector injector = Guice.createInjector(new MyServletModule()); // injector.getInstance(ServletContext.class) &lt;-- won't work yet! // BIND HERE, but note the ServletContext will not be in the injector servletContext.addListener(injector.getInstance(MyListener.class)); // (or alternatively, store the injector as a member field and // bind in contextInitialized above, after the super call) return injector; } } </code></pre> <p>Please note, in all combinations above, the listeners are not guaranteed to be called within the GuiceFilter pipeline. So, in particular, though it may work, I would suggest that you do not rely on access to Request-scoped objects, including the HttpServletRequest and HttpServletResponse, in your listeners.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload