15.AOP优化转账案例

15.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;
 }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
org.aopalliance.aop是一个Java领域的开源项目,是AspectJ和其他AOP框架之间的一个接口标准化项目。AOP(面向切面编程)是一种编程范式,它允许开发人员将通用的业务逻辑与应用程序的特定功能进行分离,从而提高代码的可重用性和可维护性。 org.aopalliance.aop项目的主要目的是定义一组接口,用于描述AOP框架的核心概念,例如切面(Aspect)、连接点(Join point)和通知(Advice)等。这些接口提供了一种标准的方式来表示和操作切面逻辑,从而使不同的AOP框架之间能够互相兼容。 要下载org.aopalliance.aop项目,可以访问其官方网站或开源代码托管平台,如GitHub等。从官方网站下载通常会提供编译好的二进制文件(JAR包)以及相关的文档。如果需要查看或参与开发,也可以访问项目的代码托管平台获取源代码。 下载org.aopalliance.aop项目后,可以将其导入Java开发环境,并添加到自己的项目依赖中。通过使用org.aopalliance.aop提供的接口,开发人员可以轻松地定义和应用切面逻辑,以实现应用程序中的横切关注点(cross-cutting concerns)。这样可以大大简化代码,提高系统的灵活性和可重用性。 总之,org.aopalliance.aop项目是一个重要的AOP接口标准化项目,通过定义一组接口,实现不同AOP框架之间的兼容性,从而帮助开发人员更加方便地使用AOP编程范式,提高代码质量和开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员阿红

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值