AOP优化转账案例

image-20220307164830703

TransactionManager:切面类

1.xml配置实现

1.1 配置文件

<?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"
       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
">

<!--    开启注解扫描-->
    <context:component-scan base-package="com.lagou"></context:component-scan>

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

<!--    配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <!--配置QueryRunner,它是jar包封装好的,实例化时需要传入数据源作为参数-->
    <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
        <constructor-arg name="ds" ref="dataSource"></constructor-arg>
    </bean>

<!--    AOP配置-->
    <aop:config>
<!--        切点表达式-->
        <aop:pointcut id="myPointcut" expression="execution(* com.lagou.service.Impl.AccountServiceImpl.*(..))"/>
<!--        配置切面-->
        <aop:aspect ref="transactionManager">
            <aop:before method="beginTransacation" pointcut-ref="myPointcut" />
            <aop:after-returning method="conmmit" pointcut-ref="myPointcut" />
            <aop:after-throwing method="rollback" pointcut-ref="myPointcut" />
            <aop:after method="release" pointcut-ref="myPointcut" />
        </aop:aspect>
    </aop:config>
</beans>

1.2 事务管理器(通知)

package com.lagou.util;

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

import java.sql.SQLException;

/**
 * 事务管理器工具类,包含:开启事务、提交事务、回滚事务、释放资源
 */
@Component
public class TransactionManager {
    @Autowired
    private ConnectionUtils connectionUtils;

//    设置事务为手动提交
    public void beginTransacation(){
        try {
            connectionUtils.getThreadConnection().setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
//    提交事务
    public void conmmit(){
        try {
            connectionUtils.getThreadConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
//    回滚事务
    public void rollback(){
        try {
            connectionUtils.getThreadConnection().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
//    释放资源
    public void release(){
        try {
            connectionUtils.getThreadConnection().setAutoCommit(true);//设置会自动提交事务
            connectionUtils.getThreadConnection().close();//关闭连接
            connectionUtils.removeThredConnection();//接触线程绑定
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

1.3切入点

    @Override
    public void transfer(String name1, String name2,Double money) {


//        2.业务操作
            //一个账号转出资金
            accountDao.out(name1,money);
            int num = 100;
            num = num / 0;
            //另一个账号资金就增加
            accountDao.in(name2 ,money );


    }

2.基于注解实现

2.1配置文件

<?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">
  <!--开启组件扫描-->
  <context:component-scan base-package="com.lagou"/>
  <!--开启AOP注解支持-->
  <aop:aspectj-autoproxy/>
 
  <!--加载jdbc配置文件-->
  <context:property-placeholder location="classpath:jdbc.properties"/>
  <!--把数据库连接池交给IOC容器-->
  <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>
  <!--把QueryRunner交给IOC容器-->
  <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
    <constructor-arg name="ds" ref="dataSource"></constructor-arg>
  </bean>
</beans>

2.2事务管理器(通知)

@Component
@Aspect
public class TransactionManager {
  @Autowired
  ConnectionUtils connectionUtils;
  @Around("execution(* com.lagou.serivce..*.*(..))")
  public Object around(ProceedingJoinPoint pjp) {
    Object object = null;
    try {
      // 开启事务
      connectionUtils.getThreadConnection().setAutoCommit(false);
      // 业务逻辑
      pjp.proceed();
      // 提交事务
      connectionUtils.getThreadConnection().commit();
   } catch (Throwable throwable) {
      throwable.printStackTrace();
      // 回滚事务
      try {
        connectionUtils.getThreadConnection().rollback();
     } catch (SQLException e) {
        e.printStackTrace();
     }
   } finally {
      try {
        connectionUtils.getThreadConnection().setAutoCommit(true);
        connectionUtils.getThreadConnection().close();
        connectionUtils.removeThreadConnection();
     } catch (SQLException e) {
        e.printStackTrace();
     }
   }
    return object;
 }
}

💖💖💖 完结撒花

💖💖💖 路漫漫其修远兮,吾将上下而求索

💖💖💖 写作不易,如果您觉得写的不错,欢迎给博主点赞、收藏、评论、收藏来一波~让博主更有动力吧

✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨

最后,还不收藏进你的收藏夹吃灰😎😎😎