代码如下:
public class SessionListenerUtil implements HttpSessionListener{
/**
* 用户连接同步 哈希表
*/
public final static Map<String, HttpSession> userSessions = Collections
.synchronizedMap(new HashMap<String, HttpSession>());
private Logger log = Logger.getLogger(SessionListenerUtil.class);
// private static WebApplicationContext context = ContextLoader
// .getCurrentWebApplicationContext();
//SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
// @Autowired
// private UserService userService;
@Override
public void sessionCreated(HttpSessionEvent event) {
log.info("session created."+"id:"+event.getSession().getId());
//userSessions.put(event.getSession().getId(), event.getSession());
log.info("sessionSize:"+SessionListenerUtil.mapSize());
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
log.info("session destroyed."+"id:"+event.getSession().getId());
HttpSession session = event.getSession();
ServletContext servletContext = session.getServletContext();
WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
//SessionFactory sessionFactory = (SessionFactory) webApplicationContext.getBean("sessionFactory");
UserService userService = (UserService) webApplicationContext.getBean("userService");
Userinfo userInfo = (Userinfo) session.getAttribute("userInfo");
userSessions.remove(userInfo.getUserName());
// WebApplicationContext web=ContextLoader.getCurrentWebApplicationContext();
// ServletContext ctx=web.getServletContext();
// ApplicationContext application=(ApplicationContext)ctx.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher");
if(userInfo.getUserName()!=null){
//更新用户退出时间、在线状态等信息
userInfo.setOnlineState("100");//用户下线
//更新用户退出时间
userInfo.setLastLogOutTime(DateUtil.dateToTimeStamp(new Date()));
userService.update(userInfo);
String hql = " update Userinfo u set u.onlineState='100' where u.id =?";
// Query query = sessionFactory.getCurrentSession().createQuery(hql);
// query.setParameter(0, userInfo.getId());
// query.executeUpdate();
//session.invalidate();//销毁session
session.removeAttribute("userInfo");//移除session中"userInfo"对象
session.removeAttribute("listRes");//移除session中"listRes"对象
}
log.info("sessionSize:"+SessionListenerUtil.mapSize());
}
1、UserService userService = (UserService) webApplicationContext.getBean("userService");
这也取不到
No bean named 'userService' is defined
2、sessionFactory.getCurrentSession() 为空
我想做的操作是,当sessionDestory触发的时候,更新一条sql语句。
谢谢。
------解决思路----------------------
sessionFactory.getCurrentSession方法需要在hibernate.cfg.xml做如下配置(Hibernate SessionFactory中openSession和getCurrentSession方法的区别),你看看你的有没有
<propertynamepropertyname="current_session_context_class" >thread</property>
另外参考Hibernate4 No Session found for current thread原因
------解决思路----------------------
你的spring没配置好datasource或者sessionfactory吧?getcurrentsession是如果没有session,会自动创建一个session,get就是获取线程里存在的session
------解决思路----------------------
目测是spring没有配置好,从userservuce获取不到说明启动tomcat 的时候没有成功实例化这个对象。楼主耐心看一下spring配置文件
------解决思路----------------------
---------------
你获取session用不用实现sessionAware这个接口啊?如果不用..那就你再问问人吧,我也不清楚了
------解决思路----------------------
注入进来即可。
@AutoWired
直接从你Spring的配置文件里面取sessionFactory