ssh结构下,新加了一个继承HttpSessionBindingListener的监听类,用来统计访问情况.
之前都是通过spring的配置文件,配置各种bean的关系,来获得hibernate的session,已进行数据库操作
现在想在新的监听类里进行数据库操作,但这个类没有配在spring配置文件里,应该怎样获得hibernate的可用session?

解决方案 »

  1.   

    WebApplicationContextUtils可以获取到spring的ApplicationContext,想要什么对象自己取
      

  2.   

    HttpSessionBindingListener的监听类中:
    ApplicationContext ctx = new ClassPathXmlApplicationContext(
    "context.xml");
    UserDao dao = (UserDao) ctx.getBean("sessionFactory");
    Session s = sessionFactory.openSession();
      

  3.   

    那你的程序就自己写一个单列的类来加载context.xml文件啊,然后在程序中调用。或者把你的sessionFactory的配置放在一个单独的文件,HttpSessionBindingListener只重复加载那个文件
      

  4.   

    Session session = SessionFactoryUtils.getSession(getSessionFactory(), false); 
      

  5.   

    再请问,我的spring配置文件路径是\WEB-INF\applicationContext.xml.
    用ClassPathXmlApplicationContext找不到,应该怎么写?
      

  6.   

    问下,6楼的写法,需要我的listener类继承哪个类,或实现哪个接口?
      

  7.   

    你可以通过struts标签来获取
    简单一点的就是用${sessioin存的参数名}
      

  8.   

    再请问,我的spring配置文件路径是\WEB-INF\applicationContext.xml. 
    用ClassPathXmlApplicationContext找不到,应该怎么写?是不是放在/WEB-INF/applicationContext.xml里面呢
    不然 ClassPathXmlApplicationContext怎么会找不到呢
      

  9.   


    不需要,这个是 spring-hibernate3.jar包里面的方法。
      

  10.   

    我在InitListener里,加了下面这些代码import org.hibernate.Session;
    import org.springframework.orm.hibernate3.SessionFactoryUtils;Session session = SessionFactoryUtils.getSession(getSessionFactory(), false); 但报错了"没有为类型 InitListener 定义方法 getSessionFactory()"怎么解决呢?
      

  11.   

    楼主直接放在src目录下吧  可以直接获得ClassPathXmlApplicationContext("applicationContext.xml")