Spring 与 Struts Hibernate 的集成 丁勇 Email:18442056@QQ.com
学习目标 掌握 Spring 与 Struts 的集成 掌握 Spring 与 Hibernate 的集成 学会使用 Spring 实现声明式事务
Spring 与 Hibernate 集成 使用 Spring 简化 Hibernate 编程 使现有使现有 Java Java EE EE 技术更易用 Spring 的目标的目标 使用使用 Hibernate 的繁琐步骤 import org.springframework.orm.hibernate3.support.hibernated aosupport; public class FwxxDAOHibImpl extends HibernateDaoSupport implements FwxxDAO{ public void add(fwxx fwxx) { 使用使用 Spring 对 Hibernate 支持支持 super.gethibernatetemplate().add(fwxx); }
Spring 与 Hibernate 集成 public class FwxxDAOHibImpl extends HibernateDaoSupport implements FwxxDAO { 使用 Spring 简化 Hibernate 编程...... public FWXX get(int fwid) { return (FWXX) super.gethibernatetemplate().get(fwxx.class, fwid); } public List search(final FWXX condition){ return super.gethibernatetemplate().executefind( 在这里构造查询条件 new HibernateCallback() { public Object doinhibernate(session s) throws HibernateException { Criteria c = s.createcriteria(fwxx.class); if (null!=condition) { // 构造 Criteria 查询条件的代码...... } c.addorder(order.asc("custcode")); return c.list(); } }); }
Spring 与 Hibernate 集成 Session 在哪里创建? FwxxDAOHibImpl 中 HibernateDaoSupport 提供了提供了如何创建 session 的? setsessionfactory 方法方法 Spring 提供了提供了 LocalSessionFactoryBean 用于创建 SessionFactory, 但需要通过数据源数据源 setdatasource 设置数据源 (DataSource) 配置数据源和 SessionFactory 使用 Spring 依赖注入, 只需配置, 无需编码
Spring 与 Hibernate 集成 依赖注入顺序 datasource sessionfactory fwxxdao fwxxbiz fwxxaction <bean id="datasource" 配置数据源 class="org.apache.commons.dbcp.basicdatasource" destroy-method="close"> <property name="driverclassname" value="com.microsoft.jdbc.sqlserver.sqlserverdriver" /> <property name="url" value="jdbc:microsoft:sqlserver://localhost;databasename=epet;selectmetho 数据库连接信息 d=cursor" /> <property name="username" value="sa" /> <property name="password" value="pwd" />
Spring 与 Hibernate 集成 配置 SessionFactory <bean id="sessionfactory" class="org.springframework.orm.hibernate3.localsessionfactor 注入 ybean"> datasource <property name="datasource" ref="datasource" /> <property name="mappingresources"> <list> <value>com/aptech/jb/entity/user.hbm.xml</value> Hibernate <value>com/aptech/jb/entity/fwxx.hbm.xml</value> 映射文件列... 表 </list> </property> Hibernate 属 <property name="hibernateproperties"> 性 <props> <prop key="hibernate.dialect"> org.hibernate.dialect.sqlserverdialect 演示示例 </prop> :Spring 与 Hibernate 集成集成 <prop key="hibernate.show_sql">true</prop> </props>
小结 继承自 HibernateDaoSupport 实现 UserDAO 接口 package com.aptech.jb.dao; public interface UserDAO{ public User get(int id); public void add(user item); public void del(int id); public void update(user item); public List search(final User condition); }
datasource 使用 Spring 重新组装 Web 程序 使用 Spring 管理依赖 Web 应用中组件间的依赖关系 sessionfactory fwxxdao fwxxbiz fwxxaction 组装 sessionfactory 注入到 DAO 中 <bean id="fwxxdao" class="com.aptech.jb.dao.hibimpl.fwxxdaohibim pl"> <property name="sessionfactory" ref ="sessionfactory" /> DAO 注入到 Biz/Service 中 : 首先增加 setter 方法, 去掉代码中对接口实现的依赖 <bean id="fwxxbiz" class="com.aptech.jb.biz.impl.fwxxbizi mpl"> <property name="fwxxdao" ref ="fwxxdao" />
使用 Spring 重新组装 Web 程序 与 Struts 集成 回顾 :Struts 机制 请求 ActionServlet Action Action Bean 是由 Struts 创建的 我们用 Spring 创建 Action, 告诉 Struts 到 Spring 的 Bean 工厂中去取
使用 Spring 重新组装 Web 程序 与 Struts 集成 步骤 1. 在 struts config.xml 配置插件 <?xml version="1.0" encoding="gbk"?> <struts-config>...... <message-resources parameter="com.aptech.jb.applicationresources" /> <plug-in classname="org.springframework.web.struts.contextloade rplugin"> <set-property property="contextconfiglocation" Spring 配置文件路径 value="classpath:applicationcontext.xml" /> </plug-in> </struts-config>
使用 Spring 重新组装 Web 程序 与 Struts 集成 步骤 2. 修改 Action Bean 配置 <?xml version="1.0" encoding="gbk"?> <struts-config> <action name="fwxxform" path="/fwxx" parameter="operate" scope="request" type="org.springframework.web.struts.delegatingac 修改 type 属 tionproxy"> 性 <forward name="list" path="/list.jsp" /> <forward name="detail" path="/detail.jsp" /> <forward name="modify" path="/modify.jsp" /> <forward name="error" path="/error.jsp" /> </action> </struts-config>
使用 Spring 重新组装 Web 程序 与 Struts 集成 步骤 3. 在 Spring 配置文件中配置 Action Bean, 将 Biz 注入 name 属性与 Struts 配置中 path 属性 相同 <bean name="/fwxx" class="com.aptech.jb.web.action.fwxxac <property name="fwxxbiz" ref="fwxxbiz" /> 演示示例 :Spring 与 Struts 集成集成
小结 写出 fwxx 相关的从 datasource 到 fwxxaction 的所有接口和类, 并在 Spring 配置文件中 datasource 配置 sessionfactory fwxxdao fwxxbiz 在此基础上, 增加 user 相关类的配置 fwxxaction 提示 : datasource 和 sessionfacotory 的配置可以重用
声明式事务 事务管理 在业务逻辑层控制事务 一个业务过程通常需要多次数据库操作 事务控制是企业应用系统开发中重要问题 传统方式 散布在系统中的事务处理代码 务处理代码 采取 AOP 的方式实现声明式事务 传来传去的 Connection / Session 对象 AOP 方式独立于系统代码之外采取声明 配置的方式给系统提供事务支持
1.x 的方式 声明式事务 回顾 :Spring 中 AOP 的配置方式 配置思路 : 事务管理器 myhibtransactionmana ger 被代理的 Bean fwxxbiztarget 代理 Bean 注入 fwxxbiz 原 Bean 通知 (Advice) 代理 <bean id="myhibtransactionmanager" 事务管理器 class="org.springframework.orm.hibernate3.hibernatetransaction <property 配置代码 name="sessionfactory" : ref="sessionfactory"/> <bean id="fwxxbiztarget" 被代理的 Bean class="com.aptech.jb.biz.impl.fwxxbizimpl"> <property name="fwxxdao" ref ="fwxxdao" />
配置代码 : 声明式事务 代理类 <bean id="fwxxbiz" class="org.springframework.transaction.interceptor.transaction ProxyFactoryBean"> <property name="transactionmanager" ref="myhibtransactionmanager"/> <property name="target" ref="fwxxbiztarget" /> <property name="transactionattributes"> <props> <prop key="add*">propagation_required </prop> 声明事务属性 <prop key="del*">propagation_required </prop> <prop key="update*">propagation_required </prop> <prop key="do*">propagation_required </prop> <prop key="*">propagation_required,readonly</prop> 演示示例 : 使用使用 Spring 1.x 1.x 配置声明式事务含义 : 对 add del update do 打头的 </props> 方法要求在事务环境中运行, 对其他方法在 </property> 只读事务中运行 被代理的 Bean
1.x 的方式 存在问题 系统中存在多个业务逻辑对象时 声明式事务 反反复复地定义 target 和代理和代理 bean bean 反反复复地声明事务 解决办法 : 定义 basetransproxy <bean id="basetransproxy" abstract="true" class="..."> <property name="transactionmanager" 没有注入 ref="myhibtransactionmanager"/> target <property name="transactionattributes"> <props>...... </props> </property> 定义为 抽象的
声明式事务 继承自 basetransproxy <!-- Target --> <bean id="fwxxbiztarget" class="com.aptech.jb.biz.impl.fwxxbizimpl"> <property name="fwxxdao" ref="fwxxdao" /> <bean id="userbiztarget" class="com.aptech.jb.biz.impl.userbizimpl"> <property name="userdao" ref="userdao" /> <!-- Biz --> bean <bean id="fwxxbiz" parent="basetransproxy"> <property name="target" ref=" fwxxbiztarget" /> <bean id="userbiz" parent="basetransproxy"> <property name="target" ref=" userbiztarget" /> 定义 target 定义代理
2.0 的方式 添加 Spring 2.0 到项目 1 添加 Spring 2.0 的 jar 包 声明式事务 <?xml 2 添加 version="1.0" Spring 2.0 encoding="utf-8"?> 配置文件 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschemainstance" xmlns:aop="http://www.springframework.org/schema/ aop" xmlns:tx="http://www.springframework.org/schema/tx " xsi:schemalocation="http://www.springframework.org/ schema/beans http://www.springframework.org/schema/beans/springbeans-2.0.xsd http://www.springframework.org/schema/tx
声明式事务 2.0 的方式 优势 : 更简洁易懂 非侵入式 配置思路 : 事务管理器 myhibtransactionmanag er 事务通知 txadvice 切面 bizmetho ds 通知者 (advisor)
2.0 的方式 声明式事务 配置代码 : <tx:advice id="txadvice" transaction-manager="myhibtransactionmanager"> <tx:attributes> <tx:method name="add*" propagation="required" 声明事 /> 务属性 <tx:method name="del*" propagation="required" /> <tx:method name="update*" propagation="required" /> <tx:method name="do*" propagation="required" /> <tx:method name="*" propagation="supports" 应用于 biz 层的所 read-only="true" /> 有类的所有方法 </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="bizmethods" 将事务属性和应用范围关联
总结 使用 Spring 如何简化了 Hibernate 编码? Spring 如何与 Struts 集成? 如何使用 Spring 实现声明式事务? (1.x 的方式,2.x 的方式 )