SSH配置声明式事务管理

1.首先配置transactionManager

<!-- 配置事务管理器(声明式的事务) -->  

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

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

    </bean>

<1>使用tx标签方式

<!-- 1.使用tx标签方式 --><!--第一种配置事务的方式 ,tx  -->

      <aop:aspectj-autoproxy proxy-target-class="true"/>

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

    <tx:attributes>  

        <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />  

        <tx:method name="modify*" propagation="REQUIRED" rollback-for="Exception" />  

        <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>  

        <tx:method name="*" propagation="REQUIRED" read-only="true"/>  

    </tx:attributes>  

</tx:advice>  

  

<aop:config>  

    <aop:pointcut id="daoMethod" expression="execution(* com.suo.dao.*.*(..))"/>  

    <aop:advisor pointcut-ref="daoMethod" advice-ref="txadvice"/>  

</aop:config> 

<2>使用代理方式

    <!--2.代理方式  -->

     <bean id="transactionProxy"  

        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  lazy-init="true" abstract="true">

  <property name="transactionManager" ref="transactionManager" />

  <property name="transactionAttributes">

  <props>

     <prop key="modify*">PROPAGATION_REQUIRED</prop>

   <prop key="delete*">PROPAGATION_REQUIRED</prop>

       <prop key="add*">PROPAGATION_REQUIRED</prop>

          <prop key="*">PROPAGATION_REQUIRED</prop>

    </props>

    </property>

  </bean>

  <bean id="userDaoAgency" parent="transactionProxy">  

        <property name="target" ref="userDao" />  

    </bean> 

<3>使用拦截器

 <!-- 3.使用拦截器-->

      <bean id="transactionInterceptor"  

        class="org.springframework.transaction.interceptor.TransactionInterceptor">

  <property name="transactionManager" ref="transactionManager" />

  <property name="transactionAttributes">

   <props>

     <prop key="modify*">PROPAGATION_REQUIRED</prop>

    <prop key="delete*">PROPAGATION_REQUIRED</prop>

      <prop key="add*">PROPAGATION_REQUIRED</prop>

      <prop key="*">PROPAGATION_REQUIRED</prop>

   </props>

   </property>

 </bean>


 <bean  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  

 <property name="proxyTargetClass">  

   <value>true</value>  

  </property>  

        <property name="beanNames">  

            <list>  

                <value>userDao</value>  

            </list>  

        </property>  

        <property name="interceptorNames">  

            <list>  

                <value>transactionInterceptor</value>  

            </list>  

        </property>  

    </bean>

2.整个配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop" 

    xmlns:tx="http://www.springframework.org/schema/tx" 

    xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/tx 

                     http://www.springframework.org/schema/tx/spring-tx.xsd 

                     http://www.springframework.org/schema/aop 

                     http://www.springframework.org/schema/aop/spring-aop.xsd">



<bean id="dataSource"

class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName"

value="com.mysql.jdbc.Driver">

</property>

<property name="url" value="jdbc:mysql://localhost:3306/test"></property>

<property name="username" value="root"></property>

<property name="password" value="root"></property>

</bean>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource">

<ref bean="dataSource" />

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</prop>

</props>

</property>

<property name="mappingResources">

            <list>

                <value>com/model/User.hbm.xml</value>

            </list>

        </property>

</bean>

<bean id="loginAction" class="com.suo.action.LoginAction">

<property name="userService" ref="userService"></property>

</bean>

<bean id="userService" class="com.suo.service.UserService">

        <property name="userDao" ref="userDao"></property>

    </bean>

    <bean id="userDao" class="com.suo.dao.UserDao">

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

    </bean>

<!-- 配置事务管理器(声明式的事务) -->  

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

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

    </bean>

      <!-- 配置事务的传播特性 -->

       <!-- 2.使用拦截器--><!--

      <bean id="transactionInterceptor"  

        class="org.springframework.transaction.interceptor.TransactionInterceptor">

  <property name="transactionManager" ref="transactionManager" />

  <property name="transactionAttributes">

   <props>

     <prop key="modify*">PROPAGATION_REQUIRED</prop>

    <prop key="delete*">PROPAGATION_REQUIRED</prop>

      <prop key="add*">PROPAGATION_REQUIRED</prop>

      <prop key="*">PROPAGATION_REQUIRED</prop>

   </props>

   </property>

 </bean>


 <bean  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  

 <property name="proxyTargetClass">  

   <value>true</value>  

  </property>  

        <property name="beanNames">  

            <list>  

                <value>userDao</value>  

            </list>  

        </property>  

        <property name="interceptorNames">  

            <list>  

                <value>transactionInterceptor</value>  

            </list>  

        </property>  

    </bean>

    

    --><!-- 1.使用tx标签方式 --><!--

     第一种配置事务的方式 ,tx  -->

     <!-- <aop:aspectj-autoproxy proxy-target-class="true"/>

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

    <tx:attributes>  

        <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />  

        <tx:method name="modify*" propagation="REQUIRED" rollback-for="Exception" />  

        <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>  

        <tx:method name="*" propagation="REQUIRED" read-only="true"/>  

    </tx:attributes>  

</tx:advice>  

  

<aop:config>  

    <aop:pointcut id="daoMethod" expression="execution(* com.suo.dao.*.*(..))"/>  

    <aop:advisor pointcut-ref="daoMethod" advice-ref="txadvice"/>  

</aop:config>  -->

    

    <!--3.代理方式  -->

     <bean id="transactionProxy"  

        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  lazy-init="true" abstract="true">

  <property name="transactionManager" ref="transactionManager" />

  <property name="transactionAttributes">

  <props>

     <prop key="modify*">PROPAGATION_REQUIRED</prop>

   <prop key="delete*">PROPAGATION_REQUIRED</prop>

       <prop key="add*">PROPAGATION_REQUIRED</prop>

          <prop key="*">PROPAGATION_REQUIRED</prop>

    </props>

    </property>

  </bean>

  <bean id="userDaoAgency" parent="transactionProxy">  

        <property name="target" ref="userDao" />  

    </bean> 

</beans>


转载于:https://my.oschina.net/yangfeima/blog/661979

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值