Spring+Hibernate声明式事务

配置声明是式事务首先把hibernate.cfg.xml加载进来配置SessionFactory,在Spring中如下配置:

<bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <!--property中的name值固定为configLocation
       -->
      <property name="configLocation">
           <value>classpath:hibernate.cfg.xml</value>
      </property>
  </bean>
接着配置事务管理器:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <!--
        property中的name属性固定为session,ref中的bean为上面的bean中的id值
     -->
      <property name="sessionFactory">
         <ref bean="sessionFactory"/>
      </property>
   </bean>
然后配置事务的传播特性如下:

   <!--transaction-manager中的值为上面的事务管理器的id值,在这里tx:advice可以理解为AOP中的advice,相当于方法的拦截,这里只是加上声明式事务
    -->
   <tx:advice id="txadvice" transaction-manager="transactionManager">
      <tx:attributes>
           <tx:method name="add*" propagation="REQUIRED"/>
           <tx:method name="*" read-only="true"/>
      </tx:attributes> 
   </tx:advice>
这里配置AOP:
   <!--pointcut中的expression中的表达式为具体的方法
       advice-ref中指向刚才的tx:advice
       pointcut-ref指向的事这里定义的pointcut
    -->
  <aop:config>
      <aop:pointcut id= "addallmethod" expression="execution(*     com.sun.manager.*.*(..))"/>
      <aop:advisor pointcut-ref="addallmethod" advice-ref="txadvice"/>
  </aop:config>
到此整个声明式事务配置完毕!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值