基于注解方式配置声明式事务

基于注解方式配置声明式事务

一、注解+xml的形式

xml:

java 复制代码
<?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
       ">
    <!--组件扫描-->
    <context:component-scan base-package="com.itheima"/>

    <!--加载properties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--配置数据源信息-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--配置SqlSessionFactoryBean,作用将SqlSessionFactory存储到spring容器-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--MapperScannerConfigurer,作用扫描指定的包,产生Mapper对象存储到Spring容器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.mapper"></property>
    </bean>

    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
<!--配置事务代理(实际上就是注解驱动)-->
    <tx:annotation-driven transaction-manager/>
</beans>

注解部分:

java 复制代码
package com.itheima.service.impl;

import com.itheima.mapper.AccountMapper;
import com.itheima.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("accountService")
@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
public class AccountServiceImpl2 implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public void transferMoney(String outAccount, String inAccount, Integer money) {
//        如果是编程类事务的话:先开一个事务,然后try catch以下有异常就回滚
        accountMapper.decrMoney(outAccount,money);
        int i = 1/0;
        accountMapper.incrMoney(inAccount,money);
    }

    public void registAccount(){

    }
}

但是我们要是改配就会报错:

java 复制代码
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager222">
        <property name="dataSource" ref="dataSource"/>
    </bean>

这是因为其底层在找一个名为"transactionManager"的事务管理器,这样配置指定好就不会报错了:

java 复制代码
 <tx:annotation-driven transaction-manager="transactionManager222"/>

二、全注解方式

其实全注解方式就是使用核心配置类来进行操作,之前也是讲过,只不过这里是多了一个事务的注解:@EnableTransactionManagement,相关与xml中的:

java 复制代码
 <tx:annotation-driven transaction-manager/>
java 复制代码
package com.itheima.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

//使用核心配置类来实现全注解开发
//开启组件扫描
@ComponentScan("com.itheima")
//注册为核心配置类
@Configuration
//开启事务
@EnableTransactionManagement
//接口扫描
@MapperScan("com.itheima.mapper")
//开启资源加载
@PropertySource("classpath:jdbc.properties")
public class SpringConfig2 {
//    先是配置数据源:使用构造方法的形式
//    注册成为bean
    @Bean
    public DataSource dataSource(
//            使用注解的形式传参进行属性注入
            @Value("${jdbc.username}") String username,
            @Value("${jdbc.password}") String password,
            @Value("${jdbc.driver}") String driver,
            @Value("${jdbc.url}") String url
            ){
//        先创建一个指定数据源,然后再进行属性设置
       DruidDataSource dataSource = new DruidDataSource();
       dataSource.setUsername(username);
       dataSource.setDriverClassName(driver);
       dataSource.setPassword(password);
       dataSource.setUrl(url);
       return dataSource;
    }

//    再是配置SqlSessionFactoryBean
//    一样是先注册成为Bean
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new  SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }

//    配置事物管理
    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource){
        DataSourceTransactionManager dataSourceTransactionManager  = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;
    }
}

常见误区答疑:

相关推荐
源码宝21 小时前
MES系统源码:Java8 + SpringBoot2.7 + MySQL8 + Redis,后端源码清爽易扩展
java·后端·源码·springboot·mes系统·源码二开·mes源码
JAVA社区21 小时前
Java高级全套教程(十)—— SpringCloudAlibaba超详细实战详解
java·开发语言·spring cloud·面试·职场和发展
弥树子21 小时前
踩坑记录:服务器内网调用接口,真实请求URL与官方公开URL不一致问题排查
开发语言·php
金銀銅鐵21 小时前
[Java] 如何理解 class 文件中方法的 descriptor?
java·后端
村口张大爷21 小时前
05 — 分层架构与依赖倒置
后端·架构·系统架构
云烟成雨TD21 小时前
Spring AI Alibaba 1.x 系列【63】AI Agent 长期记忆
java·人工智能·spring
z落落1 天前
C# ToCharArray + foreach遍历 + String与StringBuilder
开发语言·c#
憧憬成为java架构高手的小白1 天前
苍穹外卖--day09
java·spring boot·百度
学代码的真由酱1 天前
Java多用户一对一网页聊天室-测试报告
java·开发语言·功能测试·测试
人道领域1 天前
【LeetCode刷题日记】669.修剪二叉搜索树
开发语言·python·算法