您的位置:首页 > 移动开发

spring配置文件application.xml中的事务总结

2010-02-02 11:36 453 查看
spring配置文件中几种配置事务的方式:

第一种:通过aop管理用户的增删改 (aspectJ)
 <aop:config>
  <aop:advisor pointcut="execution(* *..OrderDao.*(..))"
   advice-ref="txAdvice" />
 </aop:config>
 <tx:advice id="txAdvice">
  <tx:attributes>
   <tx:method name="save*" />
   <tx:method name="update*" />
   <tx:method name="delete*" />
   <tx:method name="*" read-only="true" />
  </tx:attributes>
 </tx:advice>

第二种:通过拦截器
Dao实现类实例
<bean name="dao" class="org.itfuture.www.dao.impl.OrderDaoImpl">
  <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
 事务bean
<bean id="baseproxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager" ref="transactionManager"/>
  <property name="target" ref="dao"/>//所在拦截的目标指向
  <property name="transactionAttributes">//拦截目标内的哪些方法
   <props>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
</bean>
创建orderservice子类(实现类)的实例,注入 orderDao的实现类orderimpl的实例
<bean name="orderservice"
 class="org.itfuture.www.service.impl.OrderServiceImpl">
 <property name="dao" ref="baseproxy"></property>//引用baseproxy代理
</bean>
第三种配置:aop与interceptor相接合
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
     <property name="transactionManager" ref="transactionManager">//引用transactionManager
     </property>
     <property name="transactionAttributes">//定义哪些方法是要用到事务的,
       <props>
      <prop key="delete*">PROPAGATION_REQUIRED</prop>
      <prop key="save*">PROPAGATION_REQUIRED</prop>
      <prop key="update*">PROPAGATION_REQUIRED</prop>
      <prop key="add*">PROPAGATION_REQUIRED</prop>
      <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
       </props>
     </property>
 </bean>
 <bean id="auto" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
   <property name="interceptorNames">
     <list>
        <value>transactionInterceptor</value>
     </list>
   </property>
 </bean>
第四种配置:个人认为比较好用这种

<bean id="dao" class="org.itfuture.www.dao">//把所有的*dao上升到接口
  <property name="accountDao" ref="accountDao"/>
  <property name="categoryDao" ref="categoryDao"/>
  <property name="productDao" ref="productDao"/>
  <property name="itemDao" ref="itemDao"/>
  <property name="orderDao" ref="orderDao"/>
 </bean>
 <aop:config>
  <aop:advisor pointcut="execution(* *..interfaceDao.*(..))" advice-ref="txAdvice"/> //然后在拦截接口
 </aop:config>
 <tx:advice id="txAdvice">
  <tx:attributes>
   <tx:method name="insert*"/>//拦截接口中的以insert、update、update开头的所有方法
   <tx:method name="update*"/>
   <tx:method name="*" read-only="true"/>//其它方法,只read-only
  </tx:attributes>
 </tx:advice>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息