Spring框架第六章之声明式事务(注解开发)

spring框架第六章之声明式事务(注解开发)

上一篇简单实现了xml配置的事务管理,这篇主要讲解基于注解开发的声明式事务,所以事务的四大特性和事务隔离性,传播性等,在此不再赘述,如果有小伙伴需要查看,可以访问https://blog.csdn.net/weixin_43762091/article/details/106844552 点此跳转

注解开发的好处便是简化开发,使用注解就可以实现和配置文件一样的事情,减少大量xml配置文件。但是缺点也很明确,因为没有配置文件,所以不太好找到相对应的代码和功能。只能一个类一个类去查看。对于后期的维护、更新没有那么友好

关于事务上使用到的注解,简单介绍一下
@Transactional:可以标注在类上,可以标注在方法上。
标注在类上,意味着这个类里的所有方法都开启事务。
标注在方法上,意味着这个方法开启事务。
同样可以在注解后的括号里配置事务的隔离级别和传播性,但是要注意的是要在配置文件上加上<tx:annotation-driven />或者在配置类上加上@EnableTransactionManagement注解,用来开启事务。

代码实现

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

    <!--开启注解驱动-->
    <contexg:component-scan base-package="com.dyh"/>
    <!--配置各层接口实现类-->
    <bean id="accountDao" class="com.dyh.dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    <bean id="accountService" class="com.dyh.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>

    <!--配置数据源连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///spring"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <!--配置JDBCTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--开启事务注解驱动-->
    <tx:annotation-driven/>

    <!--这个注解等同于下面配置的所有注解-->
    
    <!--配置事务相关配置,进行事务管控 -->
    <!--<tx:advice id="txAdvice" transaction-manager="transactionManager">-->
        <!--<tx:attributes>-->
            <!--&lt;!&ndash;设置隔离级别为默认,默认是开启一个事务,如果有新的事务添加在老事务中&ndash;&gt;-->
            <!--<tx:method name="*" isolation="DEFAULT"/>-->
        <!--</tx:attributes>-->
    <!--</tx:advice>-->

    <!--&lt;!&ndash;配置aop&ndash;&gt;-->
    <!--<aop:config>-->
        <!--&lt;!&ndash;将事务交个aop管理&ndash;&gt;-->
        <!--<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.dyh.service.impl.*.*(..))"/>-->
    <!--</aop:config>-->

</beans>

注释了之前的部分配置,改成 <tx:annotation-driven/> 配置,然后在AccountServiceImpl上添加了@Transactional,这样就实现了开启事务。

package com.dyh.service.impl;

import com.dyh.dao.AccountDao;
import com.dyh.service.AccountService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    /**
     * 转账功能
     *
     * @param out    转出账户
     * @param target 转入目标账户
     * @param money  转入金额
     */
    @Override
    @Transactional
    public void transferAccounts(String out, String target, Double money) {
//        转出金额
        accountDao.out(out, money);
//        手写异常,模拟事务出错
        int i = 1/0;
//        转入金额
        accountDao.in(target, money);
    }
}

总结

在实际开发过程中,可以根据具体情况决定使用注解开发还是配置文件。当然在实际开发中大部分都是使用注解开发,毕竟方便快捷。但是基本的xml配置还是要理解和掌握的。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值