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

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

一、注解+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;
    }
}

常见误区答疑:

相关推荐
lingran__2 小时前
C语言预处理详解(C语言知识完结篇)
c语言·开发语言
wjs20242 小时前
《jQuery 滑动:深入浅出的探索与实践》
开发语言
Java面试题总结2 小时前
Spring AI 初步集成(2)-添加记忆
java·人工智能·spring
野犬寒鸦2 小时前
JVM垃圾回收机制深度解析(G1篇)(垃圾回收过程及专业名词详解)
java·服务器·jvm·后端·面试
清水白石0082 小时前
协程不是线程:深入理解 Python async/await 运行机制
java·linux·python
程序员老乔2 小时前
Java 新纪元 — JDK 25 + Spring Boot 4 全栈实战(五):FFM API,告别JNI在Spring Boot中直连推荐引擎
java·开发语言·spring boot
va学弟2 小时前
Java 网络通信编程(7):完善视频通信
java·服务器·网络
后青春期的诗go2 小时前
泛微OA-E9与第三方系统集成开发企业级实战记录(九)
java·金蝶·erp·泛微·oa·集成开发·e9
逸Y 仙X2 小时前
文章十:ElasticSearch索引字段高级属性
java·大数据·elasticsearch·搜索引擎·全文检索