struts1.2+spring2.0+hibernate3.2 整合的小示例

本文介绍了一个使用Spring框架进行事务管理的具体实例配置。该配置详细展示了如何通过XML方式配置SessionFactory、事务管理器,并定义了事务策略及通知器。此外还包含了DAO层及Service层的简单配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先上小示例效果图:

 

 

再上spring的配置文件:

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:aop="http://www.springframework.org/schema/aop"
  5.     xmlns:tx="http://www.springframework.org/schema/tx"
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans 
  7.                      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  8.            http://www.springframework.org/schema/aop 
  9.            http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  10.            http://www.springframework.org/schema/tx 
  11.            http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  12.     
  13.     <!-- 配置sessionFactory -->
  14.     <bean id="sessionFactory"
  15.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  16.         <property name="configLocation">
  17.             <value>classpath:hibernate.cfg.xml</value>
  18.         </property>
  19.     </bean>
  20.     <!-- 配置事务管理器 -->
  21.     <bean id="transactionManager"
  22.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  23.         <property name="sessionFactory">
  24.             <ref bean="sessionFactory" />
  25.         </property>
  26.     </bean>
  27.     <!-- 注释驱动事务 -->
  28.     <!-- 
  29.     <tx:annotation-driven transaction-manager="transactionManager"/>
  30.     -->
  31.         
  32.     <!-- 配置事务策略 -->
  33.     
  34.     <tx:advice id="txAdvice" transaction-manager="transactionManager">
  35.         <tx:attributes>
  36.             <tx:method name="create*" propagation="REQUIRED" />
  37.             <tx:method name="update*" propagation="REQUIRED" />
  38.             <tx:method name="delete*" propagation="REQUIRED" />
  39.             <tx:method name="*" propagation="SUPPORTS" read-only="true" />
  40.         </tx:attributes>
  41.     </tx:advice>
  42.     
  43.     <!-- 事务通知器:哪些类的哪些方法参与事务 -->
  44.     <aop:config>
  45.         <aop:advisor pointcut="execution(* com.qiujy.service.*.*(..))"
  46.             advice-ref="txAdvice" />
  47.     </aop:config>
  48.     
  49.     <!-- =========================== dao的配置 ================================ -->
  50.     <bean id="genericHibernateDaoImpl"
  51.         class="com.qiujy.dao.hibernate.GenericHibernateDaoImpl"
  52.         abstract="true">
  53.         <property name="sessionFactory" ref="sessionFactory" />
  54.     </bean>
  55.     <bean id="deptDao" class="com.qiujy.dao.hibernate.DeptDaoImpl"
  56.         parent="genericHibernateDaoImpl" />
  57.     <bean id="employeeDao"
  58.         class="com.qiujy.dao.hibernate.EmployeeDaoImpl"
  59.         parent="genericHibernateDaoImpl" />
  60.     <!-- ====================== ServiceFacade的配置 =========================== -->
  61.     <bean id="serviceFacade" class="com.qiujy.service.ServiceFacade">
  62.         <property name="deptDao" ref="deptDao" />
  63.         <property name="employeeDao" ref="employeeDao" />
  64.     </bean>
  65.     <!-- ====================== Struts Action的配置 =========================== -->
  66.     <bean id="baseAction" class="com.qiujy.web.base.BaseAction"
  67.         abstract="true">
  68.         <property name="serviceFacade" ref="serviceFacade" />
  69.     </bean>
  70.     <bean name="/dept" class="com.qiujy.web.dept.DeptAction"
  71.         parent="baseAction" scope="prototype" />
  72. </beans>

示例源代码将上传到csdn资源库中。呵呵。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值