Spring事务管理2--声明式

简述

1、Spring 的声明式事务管理在底层是建立在 AOP 的基础上。其本质是在方法前后进行拦截,然后在目标方法开始之前创建一个事务,在执行这目标方法结束后,根据执行情况提交或进行回滚事务。

2、声明式事务最大的优点就是不需通过编程的方式而进行管理事务,这样就不需要在业务逻辑代码中掺杂事务管理的代码,只需在配置文件中做相关的事务规则声明,便可将事务规则应用到业务逻辑中。

3、声明式事务不足的地方在于,与编程式事务相比,只能作用到方法级别,无法像编程式事务那样可以作用到代码块级别。

XML方式

<!-- 配置事务管理器================= -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- 配置事务的增强 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 配置事务管理的规则,如隔离级别,超时信息,传播行为,是否只读, -->
            <!-- <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="find*" read-only="true"/> -->
            <tx:method name="*" propagation="REQUIRED" read-only="false"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- aop的配置=================== -->
    <aop:config>
        <!-- 配置切入点 -->
        <aop:pointcut expression="execution(* zcc.spring_tx.demo2.accountServiceImp1.*(..))" id="pointcut1"/>
        <!-- 配置切入面,以前刚学面向切面编程的时候需要手动编写一个切面类,但是事务spring支持了,不需要自己手动编写-->
        <!-- <aop:aspect ref="">
            <aop:before method=""/>
        </aop:aspect> -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
        
    </aop:config>

注解方式

  <!-- 配置事务管理器================= -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- 开启注解事务================= -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

在业务层上添加注解,注解里面也可以添加属性,比如

@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED)

详细代码如下:

tx2.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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 配置Service================ -->
    <bean id="accountService"
        class="zcc.spring_tx.demo2.accountServiceImp1">
        <property name="dao" ref="accountDao"></property>
    </bean>

    <!-- 配置Dao===================== -->
    <bean id="accountDao" class="zcc.spring_tx.demo2.accountDaoImp1">
        <!-- <property name="jdbcTemplate" ref="jdbcTemplate"></property> -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置连接池和jdbc模板 ===================== -->
    <context:property-placeholder
        location="classpath:jdbc.properties" />

    <!-- 配置c3p0连接池============ -->
    <bean id="dataSource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!-- 配置jdbc模板================ -->
    <!-- <bean id="jdbcTemplate"
        class="org.springframework.jdbc.core.JdbcTemplate">
        ==属性注入==
        <property name="dataSource" ref="dataSource"></property>
    </bean> -->
    
    <!-- 配置事务管理器================= -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- 配置事务的增强 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 配置事务管理的规则 -->
            <!-- <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="find*" read-only="true"/> -->
            <tx:method name="*" propagation="REQUIRED" read-only="false"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- aop的配置=================== -->
    <aop:config>
        <!-- 配置切入点 -->
        <aop:pointcut expression="execution(* zcc.spring_tx.demo2.accountServiceImp1.*(..))" id="pointcut1"/>
        <!-- 配置切入面,以前刚学aop的时候需要手动编写一个切面类,但是事务spring支持了,不需要自己手动编写-->
        <!-- <aop:aspect ref="">
            <aop:before method=""/>
        </aop:aspect> -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
        
    </aop:config>
</beans>

tx3.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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 配置Service================ -->
    <bean id="accountService"
        class="zcc.spring_tx.demo3.accountServiceImp1">
        <property name="dao" ref="accountDao"></property>
    </bean>

    <!-- 配置Dao===================== -->
    <bean id="accountDao" class="zcc.spring_tx.demo3.accountDaoImp1">
        <!-- <property name="jdbcTemplate" ref="jdbcTemplate"></property> -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置连接池和jdbc模板 ===================== -->
    <context:property-placeholder
        location="classpath:jdbc.properties" />

    <!-- 配置c3p0连接池============ -->
    <bean id="dataSource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    
    <!-- 配置事务管理器================= -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- 开启注解事务=================== -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

补充:

重点掌握声明式配置

xml的,配置事务管理器,<tx:advice>,<tx:attributes>,<tx:method>,<aop:config>,,<aop:pointcut>,<aop:advisor>

注解方式的:<tx:annotation-driven transaction-manager="transactionManager"/>,在业务层上一定要记得加事务的注解

最后,看到一篇写的很不错的关于spring事务的博客,在此附上地址,总结的很好:https://blog.csdn.net/trigl/article/details/50968079

 

posted @ 2019-02-21 15:25  曾聪聪  阅读(215)  评论(0编辑  收藏  举报