0

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

Community
  • 1
  • 1
Argamidon
  • 337
  • 1
  • 4
  • 15
  • I don't see you using suggestion of any of the linked answers. Did you try them. It should definitely work. – Rohit Jain Apr 18 '15 at 05:06
  • `sessionDestroyed` is not the init method. Please re-read the linked answers. – Rohit Jain Apr 18 '15 at 05:14
  • Could it be that somehow your BattleshipBeansConfiguration has not been instantiated or populated before the call to your HttpSessionDestroyed methods ? So when it tries to inject your dependency it fails because it can't find it and it can't find it because it doesn't exist yet? – TchiYuan Apr 18 '15 at 19:24

0 Answers0