I have to inject dependency into my HttpSessionListener class. I have read this one and this , but it is not working.
@Configuration(value = "battleshipBeans")
public class BattleshipBeansConfiguration {
...a lot of beans.
}
this is my listener implementation:
public class HttpSessionDestroyed implements HttpSessionListener{
@Resource(name = "battleshipBeans")
private BattleshipBeansConfiguration battleshipBeans;
@Override
public void sessionCreated(HttpSessionEvent se) {
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
}
}
In web.xml I added the last listener in the file:
<listener>
<description>HttpSessionListener</description>
<listener-class>com.pack.battleship.web.controller.HttpSessionDestroyed</listener-class>
</listener>
I tried to apply both ways. I added like this
@Override
public void sessionDestroyed(HttpSessionEvent se) {
WebApplicationContextUtils
.getRequiredWebApplicationContext(se.getSession().getServletContext())
.getAutowireCapableBeanFactory()
.autowireBean(this);
}
And added like this:
@Override
public void sessionDestroyed(HttpSessionEvent se) {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
and after I tried to start my app, BUT I got exception in both cases:
17-Apr-2015 13:20:44.461 INFO [http-nio-8080-exec-6] org.apache.catalina.core.ApplicationContext.log No Spring WebApplicationInitializer types detected on classpath
17-Apr-2015 13:20:44.601 SEVERE [http-nio-8080-exec-6] org.apache.catalina.core.StandardContext.listenerStart Error configuring application listener of class com.pack.battleship.web.controller.HttpSessionDestroyed
javax.naming.NamingException: Cannot create resource instance
UPDATE
okay, I've just updated my Listener and implemented interface ServletContextListener
. And added initialization process into this method:
@Override
public void contextInitialized(ServletContextEvent sce) {
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(
this, sce.getServletContext());
}
BUT, I have the same exception