Spring整合hibernate有兩方面:
1、由Spring IOC管理hibernate的session-factory
2、讓hibernate使用上sping的申明事物
整合步驟:
1. 加入hibernate:加入jar,hibernate的配置文件(hibernate.cfg.xml),編寫持久化類對應的hbm.xml文件
2. 加入spring:加入jar,添加applicationContext.xml
3. 整合
配置數據源、配置sessionFactory、配置hibernate文件的位置及路徑*.cfg.xml,配置hibernate的映射文件hbm.xml文件,可以使用通配符。
spring hibernate 的事物流程:
1、在方法開始前:先獲取session,然後將session綁定到當前線程中,開啓事物
2、方法正常結束:先關閉事物,然後解除當前線程與session的綁定關係,關閉session
3、出現異常: 先回滾事物,然後解除當前線程與session的綁定關係,關閉session
spring整合struts2
Spring整合struts2需要完成的任務:
原理:
1、在服務器啓動時,按照全局變量名獲取需要初始化的配置文件,在ServletContextListener/contextInitialized中獲取配置文件名,然後創建spring容器,將容器加載到application域對象的屬性中,
2、在Servlet中通過ServletContext獲取創建的spring容器,然後獲取bean。
注:
spring整合struts2的核心:使用Spring 容器來管理struts2的action