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了,是不能成功提交到數據庫的。解決辦法就是用環繞通知。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM