Spring事务管理的两种方式

参考文档:

http://www.iteye.com/topic/1123347
http://blog.csdn.net/lcj8/article/details/2835432

PS:好像还是tx标签的方式较为简单

tx标签设置拦截器:

<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
        
    <context:component-scan base-package="com.springsession.demo" />
    <context:property-placeholder location="classpath:jdbc.properties" /> 
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        
    </bean>  
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <!-- 
            <property name="mapperLocations" value="classpath*:com/springMyBatis/system.mapper/*.xml" />
         -->
        <property name="configLocation" value="classpath:mybatis-config.xml" />  
    </bean>  
    
    <bean id="testDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.springsession.demo.dao.ISpringDao" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    
    <bean id="serviceProxy" class="com.springsession.demo.service.TestService" autowire="byName" />
    
    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- 使用tx标签配置的拦截器 -->
    <!-- 定义事务 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    
    <!-- 定义切面 -->
    <aop:config>
        <aop:pointcut id="interceptorPointcut" expression="execution(* com.springsession.demo.dao.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointcut" />
    </aop:config>
    
</beans>

以代理的方式实现事务控制:

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
        
    <context:component-scan base-package="com.springsession.demo" />
    <context:property-placeholder location="classpath:jdbc.properties" /> 
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        
    </bean>  
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <!-- 
            <property name="mapperLocations" value="classpath*:com/springMyBatis/system.mapper/*.xml" />
         -->
        <property name="configLocation" value="classpath:mybatis-config.xml" />  
    </bean>  
    
    <bean id="testDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.springsession.demo.dao.ISpringDao" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    
    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 定义事务 -->
    <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="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>

    <!-- 配置代理对象,利用AOP进行拦截实现对事务的控制 -->
    <bean id="serviceProxy" parent="transactionProxy">
        <property name="target">
            <bean class="com.springsession.demo.service.TestService" autowire="byName" />
        </property>
    </bean>

</beans>
原文地址:https://www.cnblogs.com/lichmama/p/5299008.html