I want to inject spring dependency in my custom HTTPSessionListener instance by following below link.If I remove the entry from web.xml sessionDestroyed method does not get called however when the entry is there in web.xml method gets called but the dependency of XUSerMgr is null.
How to inject dependencies into HttpSessionListener, using Spring?enter code here
ring
@Component
public class RangerHttpSessionListener implements HttpSessionListener,ApplicationContextAware {
@Autowired
XUserMgr xUserMgr;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (applicationContext instanceof WebApplicationContext) {
((WebApplicationContext) applicationContext).getServletContext().addListener(this);
} else {
//Either throw an exception or fail gracefully, up to you
throw new RuntimeException("Must be inside a web application context");
}
}
private static CopyOnWriteArrayList<HttpSession> listOfSession = new CopyOnWriteArrayList<HttpSession>();
@Override
public void sessionCreated(HttpSessionEvent event) {
listOfSession.add(event.getSession());
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
if (!listOfSession.isEmpty()) {
updateIsActiveStatusForAuthSession(event.getSession());
listOfSession.remove(event.getSession());
}
}
private void updateIsActiveStatusForAuthSession(HttpSession session) {
xUserMgr.updateIsActiveStatusOfLoggedInUserWithHTTPSession(session.getId(),1);
}
public static CopyOnWriteArrayList<HttpSession> getActiveSessionOnServer() {
return listOfSession;
}
}
I have the followingentry in my web.xml. > <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <listener> <listener-class>org.apache.ranger.security.listener.RangerHttpSessionListener</listener-class> </listener>