Question

Configuration: Guice 1.0, Apache Tomcat 6.0

I am currently manually injecting objects configured in a Guice Module, into my servlet, using this method:

public void init( ServletConfig config ) throws ServletException
{
    super.init( config );
    ServletContext sc = config.getServletContext();
    Injector injector = (Injector) sc
        .getAttribute( Constants.Guice.INJECTOR_APP_CONTEXT_KEY );
    injector.injectMembers( this );
}

How can I do the same into a HttpSessionAttributeListener (since it doesn't have any lifecycle methods) ?

Was it helpful?

Solution

Event Listener is all about life-cycle events. For example, attributeAdded() is called right after an attribute is added to a session, similar to init() for Servlet.

It probably makes more sense to inject object into HttpSession. In that case, you want do it in HttpSessionListener.sessionCreated().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top