配置spring的事务管理

网上看到过很多关于spring事务管理的东西,但是原创的并不多,如果你的运气好能看到那些原创的文章恭喜你,因为我看到的不多,但一些原创的文 章里面枝叶太多,因为那些高手直接把自己的代码拷过来,所以说无疑给一些初学者或一些目前知识面有限的读者带来了很大的困难,本人就去掉枝叶,只给主干

1,配置sessionFactory

<bean id="sessionFactory" class="org.springframwork.orm.hibernate3.LocalSessionFactory">

  <property name="configLocation">

    <ref bean="classpath:hibernate.cfg.xml" />

  </property>

</bean>

2,配置事务控制器

<bean id="transactionManager"      class="org.springframework.orm.hibernate3.HibernateSessionFactoryBean">

  <property name="sessionFactory" ref="sessionFactory">

</bean>

3,配置事务的传播性

<tx:advice id="txAdvice" transaction-manager="transactionManager">

  <tx:attributes>

    <tx:method name="add*" propagation="REQUIRED" />

    <tx:method name="del*" propagation="REQUIRED" />

    <tx:method name="update*" propagation="REQUIRED" />

    <tx:mehtod name="*" read-only="true">

  </tx:attributes>

</tx:advice>

4,配置那些类的那些方法需要事务

<aop:config>

  <aop:pointcut id="allMethod" expression="execution(* com.simpl.*.*(..))" />

  <aop:advisor pointcut-ref="allMethod" advice-ref="txAdvice" />

</aop:config>

解释一下<execution>中的内容的意思:就是com.simp包下的所有类的所有方法,*正则表达式中的通配符,其中在包前面有一个*代表返回类型为任意类型。其中execution中的表达式可以用逻辑“或”连接多个表达式的

原文地址:https://www.cnblogs.com/downey/p/4928984.html