依赖注入HttpSessionListener

时间:2015-04-18 05:05:19

标签: java spring spring-mvc dependency-injection

我必须将依赖注入我的HttpSessionListener类。我看过this onethis ,但它无效。

   @Configuration(value = "battleshipBeans")
   public class BattleshipBeansConfiguration {
      ...a lot of beans.
   }

这是我的听众实施:

public class HttpSessionDestroyed implements HttpSessionListener{

    @Resource(name = "battleshipBeans")
    private BattleshipBeansConfiguration battleshipBeans;

    @Override
    public void sessionCreated(HttpSessionEvent se) {
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
    }
}

在web.xml中,我在文件中添加了最后一个侦听器:

<listener>
    <description>HttpSessionListener</description>
    <listener-class>com.pack.battleship.web.controller.HttpSessionDestroyed</listener-class>
</listener>

我尝试了两种方式。我这样添加了

@Override
public void sessionDestroyed(HttpSessionEvent se) {
     WebApplicationContextUtils
        .getRequiredWebApplicationContext(se.getSession().getServletContext())
        .getAutowireCapableBeanFactory()
        .autowireBean(this);
}

并添加如下:

@Override
public void sessionDestroyed(HttpSessionEvent se) {
   SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}

在我尝试启动我的应用程序之后,但在这两种情况下我都有例外:

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

更新 好的,我刚刚更新了我的Listener并实现了接口ServletContextListener。并在此方法中添加了初始化过程:

@Override
public void contextInitialized(ServletContextEvent sce) {
    SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(
                    this, sce.getServletContext());
}

但我有同样的例外

0 个答案:

没有答案