SSH2配置事务的两种方式

<?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:context="http://www.springframework.org/schema/context"  

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

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  

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

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  

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

<!-- 配置sessionFactory的方式 -->  

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  

<property name="configLocation">  

<value>classpath:config/hibernate.cfg.xml</value>  

</property>  

</bean>  

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

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

</bean>  

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

    <!--    

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

<tx:attributes>  

<tx:method name="add*" propagation="REQUIRED" no-rollback-for="com.exception.MyRuntimeException"/>  

<tx:method name="modify*" propagation="REQUIRED" no-rollback-for="com.exception.MyRuntimeException"/>  

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

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

</tx:attributes>  

</tx:advice>  

<aop:config>  

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

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

</aop:config>  

    -->  

<!-- 第二种配置事务的方式,代理 -->  

<bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">  

        <!-- 使用cglib方式实现动态代理  

<property name="proxyTargetClass" value="true"></property>  

         -->  

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

<property name="transactionAttributes">  

<props>  

<prop key="add*">PROPAGATION_REQUIRED, +RuntimeException</prop>  

<prop key="modify*">PROPAGATION_REQUIRED, +com.exception.MyRuntimeException</prop>  

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

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

</props>  

</property>  

</bean>  

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

<property name="target">  

<!-- 直接写bean来代替ref标签的链接方式 -->  

<bean class="com.dao.impl.UserDaoImpl">  

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

</bean>              

</property>  

</bean>  

</beans

posted @ 2015-04-25 16:16  虞秀权  阅读(585)  评论(0编辑  收藏  举报