Tuesday, March 20, 2007

How to get Spring Context in HttpSessionListener

You can access Spring application context in the HttpSessionListener :


public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
ServletContext servletContext =
httpSessionEvent.getSession().getServletContext();

WebApplicationContext appContext=
(WebApplicationContext) servletContext.
getAttribute(WebApplicationContext.
ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

//Now we can get xServis from context
IXServis servis = (IXServis )
appContext.getBean("xServis");
//do whatever you like with servis...
}