如何用spring声明式事务

1.配制sessionFactory对象
------------------------------------------------------------------------------------------------
1.1.使用hibernate配制文件作为数据源,
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocations" value="classpath:hibernate.cfg.xml"/>
</bean>
1.2.使用spring中配制的数据源文件
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
 <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
 <property name="url" value="jdbc:mysql://localhost/bank"/>
 <property name="username" value="root"/>
 <property name="password" value="root"/>
</bean> 

<bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 <property name="dataSource" ref="dataSource"/>
 <ref local="dataSource"/>
 <property name="mappingResources">
  <list>
   <value>com/bank/entity/Bank.hbm.xml</value>
  </list>
 </property>
 <property name="hibernateProperties">
  <props>
   <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
   <prop key="show_sql">true</prop>
  </props>
 </property>
</bean>
------------------------------------------------------------------------------------------------
2.配制项目的Dao类和业务服务类,因为业务服务类是引用的DAO实现,所以要将Dao注入到服务类中
------------------------------------------------------------------------------------------------

<bean id="bankDao" class="com.bank.dao.impl.BankDaoImpl">
 <property name="sessionFactory" ref="sessionFactory"></property> 
</bean>
<!-- 配置BIZ -->
<bean id="bankBiz" class="com.bank.service.impl.BankServiceImpl">
 <property name="bankDao" ref="bankDao"></property>
</bean> 
------------------------------------------------------------------------------------------------
3.配置基于Hibernate的事务管理器

------------------------------------------------------------------------------------------------
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 <!-- 事务基于的SessionFactory -->
 <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
------------------------------------------------------------------------------------------------
4.配置事务切面
------------------------------------------------------------------------------------------------

<tx:advice id="txAdvice" transaction-manager="transactionManager">
 <tx:attributes>
  <tx:method name="transMoney" propagation="REQUIRED"/>
 </tx:attributes>
</tx:advice>
------------------------------------------------------------------------------------------------
5.使用Spring2.xx声明式事务配制方法
------------------------------------------------------------------------------------------------

<aop:config>
 <!-- 事务切面要切入的切入点,如下*为访问修饰符,
 包名为:com.bank.service.impl下的所有的类,所有的方法使用都使用事务,注意(..),如方法有对数,
 一定要打..,任意个参数之意 -->
 <aop:pointcut id="txPointCut" expression="execution(* com.bank.service.impl.*.*(..))"/>
 <!-- 将事务切与与切入点结合 -->
 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
------------------------------------------------------------------------------------------------
6.使用Spring1.xx声明式事务配制方法
------------------------------------------------------------------------------------------------

<!--配置bankBiz的事务代理对象,切入事务 -->
<bean id="bankBizProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!--注入事务事务管理器-->
<property name="transactionManager" ref="transactionManager"></property>
<!--事务级别-->
<property name="transactionAttributes">
 <props>
  <prop key="transMoney">PROPAGATION_REQUIRED</prop>
 </props>
</property>
<!-- 要代理的目标对象-->
<property name="target" ref="bankBiz"></property>
</bean>
------------------------------------------------------------------------------------------------

注:学习交流.个人心得_尹当

你可能感兴趣的:(DAO,spring,AOP,bean,Hibernate)