Spring框架8:spring使用AOP实现事务控制

本系列笔记均是对b站教程https://www.bilibili.com/video/av47952931 的学习笔记,非本人原创

基于XML的AOP实现事务控制
这个接着的是day3里的基于XML和IOC的事务控制部分
这里我们使用AOP而不是代理来实现事务控制

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

    <!-- 配置数据源 -->
    <bean id="dataSoure" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 连接数据库的必备信息-->
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/jd_learning"></property>
        <property name="user" value="root"></property>
        <property name="password" value="<密码>"></property>
    </bean>
    <!-- queryRunner不能是单例对象,防止多线程出现问题-->
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
    </bean>

    <bean id="accountDao" class="com.jiading.dao.impl.AccountDaoImpl">
        <property name="runner" ref="runner"></property>
        <property name="connectionUtils" ref="connectionUtils"></property>
    </bean>

    <bean id="accountService" class="com.jiading.service.impl.AccountServiceImpl">
        <!-- 注入dao对象-->
        <property name="accountDao" ref="accountDao"></property>
    </bean>
    <!-- 配置connection工具类 ConnectionUtils-->
    <bean id="connectionUtils" class="com.jiading.utils.ConnectionUtils">
        <property name="dataSource" ref="dataSoure"></property>
    </bean>
    <!-- 配置事务管理器-->
    <bean id="txManager" class="com.jiading.utils.TransactionManager">
        <property name="connectionUtils" ref="connectionUtils"></property>
    </bean>
    <!-- 配置AOP-->
    <aop:config>
        <aop:pointcut id="pt1" expression="execution(* com.jiading.service.impl.*.*(..))"/>
        <aop:aspect id="txAdvice" ref="txManager">
            <aop:before pointcut-ref="pt1" method="beginTransaction"></aop:before>
            <aop:after pointcut-ref="pt1" method="commit"></aop:after>
            <aop:after-throwing pointcut-ref="pt1" method="rollback"></aop:after-throwing>
       <aop:after-returning pointcut-ref="pt1" method="release"></aop:after-returning>
        </aop:aspect>
    </aop:config>
</beans>

其实主要就只改了这个xml文档,删去了factory包而已。之后accountService的每一个方法都会被加上前置、后置、异常、最终通知,以实现事务。其他和day3中的部分一样

基于注解的AOP实现事务控制

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="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/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">
    <!--配置spring创建容器时要扫描的包-->
    <context:component-scan base-package="com.jiading"></context:component-scan>
    <!-- 配置数据源 -->
    <bean id="dataSoure" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 连接数据库的必备信息-->
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/jd_learning"></property>
        <property name="user" value="root"></property>
        <property name="password" value="<密码>"></property>
    </bean>
    <!-- queryRunner不能是单例对象,防止多线程出现问题-->
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
    </bean>

    <!-- 开启spring对注解aop的支持-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
package com.jiading.utils;

import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.SQLException;

/*
和事务管理相关的工具类,它包含了开启事务、提交事务、回滚事务和释放连接的方法
 */
@Component("txManager")
@Aspect
public class TransactionManager {
    @Autowired
    private ConnectionUtils connectionUtils;
    @Pointcut("execution(* com.jiading.service.impl.*.*(..))")
    private void pt1(){}
    /*
    开启事务
     */
    @Before("pt1()")
    public void beginTransaction(){
        try {
            connectionUtils.getThreadConnection().setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @AfterReturning("pt1()")
    public void commit(){
        try {
            connectionUtils.getThreadConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @AfterThrowing("pt1()")
    public void rollback(){
        try {
            connectionUtils.getThreadConnection().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @After("pt1()")
    public void release(){
        try {
            connectionUtils.getThreadConnection().close();//将连接放回连接池
            connectionUtils.removeConnection();//解绑
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

其他的几个类也要转成注解配置,删去注入时使用的set方法,改成Autowired注解
但是注意这里有一个bug,就是我们之前说过的,最终通知会先于后置通知执行,此时连接还没有commit就release了,是不能成功提交到数据库的。解决办法就是用环绕通知。

原文地址:https://www.cnblogs.com/jiading/p/12368832.html