Spring获取SessionFactory的问题

J2EE 码拜 9年前 (2015-04-08) 1239次浏览 0个评论
 

代码如下:


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语句。
谢谢。

Spring获取SessionFactory的问题
 sessionFactory.getCurrentSession()  报错:No Session found for current thread
Spring获取SessionFactory的问题
20分
sessionFactory.getCurrentSession方法需要在hibernate.cfg.xml做如下配置(Hibernate SessionFactory中openSession和getCurrentSession方法的区别),你看看你的有没有

<propertynamepropertyname="current_session_context_class" >thread</property>  

另外参考Hibernate4 No Session found for current thread原因

Spring获取SessionFactory的问题
10分
你的spring没配置好datasource或者sessionfactory吧?getcurrentsession是如果没有session,会自动创建一个session,get就是获取线程里存在的session
Spring获取SessionFactory的问题
10分
目测是spring没有配置好,从userservuce获取不到说明启动tomcat 的时候没有成功实例化这个对象。楼主耐心看一下spring配置文件
Spring获取SessionFactory的问题
引用 2 楼 save4me 的回复:

sessionFactory.getCurrentSession方法需要在hibernate.cfg.xml做如下配置(Hibernate SessionFactory中openSession和getCurrentSession方法的区别),你看看你的有没有

<propertynamepropertyname="current_session_context_class" >thread</property>  

另外参考Hibernate4 No Session found for current thread原因

我的用户管理模块是可以这样sessionFactory.getCurrentSession 取到session的,就是不知道为何在这个实现了HttpSessionListener接口的类里面无法取到。

Spring获取SessionFactory的问题
引用 3 楼 huo3204387 的回复:

你的spring没配置好datasource或者sessionfactory吧?getcurrentsession是如果没有session,会自动创建一个session,get就是获取线程里存在的session

我的用户管理模块是可以这样sessionFactory.getCurrentSession 取到session的,就是不知道为何在这个实现了HttpSessionListener接口的类里面无法取到。

配置文件没问题啊,我CRUD 都没问题,就这个类里面获取不到

直接注入SessionFactory也不行,~

Spring获取SessionFactory的问题
10分
引用 6 楼 Javainging 的回复:
Quote: 引用 3 楼 huo3204387 的回复:

你的spring没配置好datasource或者sessionfactory吧?getcurrentsession是如果没有session,会自动创建一个session,get就是获取线程里存在的session

我的用户管理模块是可以这样sessionFactory.getCurrentSession 取到session的,就是不知道为何在这个实现了HttpSessionListener接口的类里面无法取到。

配置文件没问题啊,我CRUD 都没问题,就这个类里面获取不到

直接注入SessionFactory也不行,~

—————
你获取session用不用实现sessionAware这个接口啊?如果不用..那就你再问问人吧,我也不清楚了

Spring获取SessionFactory的问题
引用 7 楼 huo3204387 的回复:
Quote: 引用 6 楼 Javainging 的回复:
Quote: 引用 3 楼 huo3204387 的回复:

你的spring没配置好datasource或者sessionfactory吧?getcurrentsession是如果没有session,会自动创建一个session,get就是获取线程里存在的session

我的用户管理模块是可以这样sessionFactory.getCurrentSession 取到session的,就是不知道为何在这个实现了HttpSessionListener接口的类里面无法取到。

配置文件没问题啊,我CRUD 都没问题,就这个类里面获取不到

直接注入SessionFactory也不行,~

—————
你获取session用不用实现sessionAware这个接口啊?如果不用..那就你再问问人吧,我也不清楚了

不用,是注入到我的BaseHibernatDAO里面的,然后我的serviceimpl都继承BaseHibernatDAO

这样都是没问题的。

Spring获取SessionFactory的问题
50分
注入进来即可。
@AutoWired 

直接从你Spring的配置文件里面取sessionFactory


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Spring获取SessionFactory的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!