|
代码如下:
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”); 2、sessionFactory.getCurrentSession() 为空 我想做的操作是,当sessionDestory触发的时候,更新一条sql语句。 |
|
|
sessionFactory.getCurrentSession() 报错:No Session found for current thread
|
|
20分 |
sessionFactory.getCurrentSession方法需要在hibernate.cfg.xml做如下配置(Hibernate SessionFactory中openSession和getCurrentSession方法的区别),你看看你的有没有
<propertynamepropertyname="current_session_context_class" >thread</property> |
10分 |
你的spring没配置好datasource或者sessionfactory吧?getcurrentsession是如果没有session,会自动创建一个session,get就是获取线程里存在的session
|
10分 |
目测是spring没有配置好,从userservuce获取不到说明启动tomcat 的时候没有成功实例化这个对象。楼主耐心看一下spring配置文件
|
|
我的用户管理模块是可以这样sessionFactory.getCurrentSession 取到session的,就是不知道为何在这个实现了HttpSessionListener接口的类里面无法取到。 |
|
|
我的用户管理模块是可以这样sessionFactory.getCurrentSession 取到session的,就是不知道为何在这个实现了HttpSessionListener接口的类里面无法取到。 配置文件没问题啊,我CRUD 都没问题,就这个类里面获取不到 直接注入SessionFactory也不行,~ |
|
10分 |
————— |
不用,是注入到我的BaseHibernatDAO里面的,然后我的serviceimpl都继承BaseHibernatDAO 这样都是没问题的。 |
|
50分 |
注入进来即可。
@AutoWired 直接从你Spring的配置文件里面取sessionFactory |