Vechace. H 2017-07-15 07:13 采纳率: 50%
浏览 1242
已采纳

在HttpsessionListener中操作数据库,getBean获取不了怎么办?在线等

在使用SpringMVC+hibernate框架时,想要在session到期后,可以更新数据库中用户在线状态。
于是我自定义一个类 SessionListener implements HttpSessionListener,并实现了方法sessionDestroyed,代码如下:
其中:HttpsessionListener的实现类也放在了Dao的包里面

 @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        // TODO Auto-generated method stub
        HttpSession session = se.getSession();
        LoginUserSessionVO user = (LoginUserSessionVO)session.getAttribute("User");
        Integer userTID = user.getUserTID();
        Timestamp logoutTime = new Timestamp(System.currentTimeMillis());
        //获取Java bean
        UserInfoTableDao userInfoDao = (UserInfoTableDao)this.getObjectFromApplication(session.getServletContext(), "UserInfoTableDaoImpl");
        //注销当前用户
        userInfoDao.setLogoutInfoByUserTID(0, logoutTime, userTID);
        System.out.println("用户已超时退出");

    }

     /** 
     * 通过WebApplicationContextUtils 得到Spring容器的实例。根据bean的名称返回bean的实例。 
     * @param servletContext  :ServletContext上下文。 
     * @param beanName  :要取得的Spring容器中Bean的名称。 
     * @return 返回Bean的实例。 
     */  
    private Object getObjectFromApplication(ServletContext servletContext,String beanName) {

        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        return applicationContext.getBean(beanName);

    }

然后运行时报错:

 严重: Session event listener threw exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'UserInfoTableDaoImpl' is defined

不知道是什么原因?网上找了很久都没找到,是bean没有get到还是bean的名字不对?应该get哪个啊?求大神解救,在线等!

  • 写回答

1条回答 默认 最新

  • 朝暮成雪 2017-07-15 08:18
    关注

    userInfoTableDaoImpl 试试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog