views:

578

answers:

1

How to inject dependencies into HttpSessionListener, using Spring and without calls, like context.getBean("foo-bar") ?

+3  A: 

You can declare your HttpSessionListener as a bean in Spring context, and register a delegation proxy as an actual listener in web.xml, something like this:

public class DelegationListener implements HttpSessionListener {
    public void sessionCreated(HttpSessionEvent se) {
        ApplicationContext context = 
            WebApplicationContextUtils.getWebApplicationContext(
                se.getSession().getServletContext()
            );

        HttpSessionListener target = 
            context.getBean("myListener", HttpSessionListener.class);
        target.sessionCreated(se);
    }

    ...
}
axtavt
That's fine solution, I wonder why there's no such DelegationListener in Spring itself.
Shaman