1. package com.hope.utils;

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

    import java.sql.SQLException;

    /**
    * @author newcityman
    * @date 2019/11/21 - 20:52
    */
    @Component("txManager")
    @Aspect
    public class TransactionManager {
    @Autowired
    private ConnectionUtils connectionUtils;
    @Pointcut("execution(* com.hope.service.impl.*.*(..))")
    private void pt(){ }

    /**
    * 开启事务
    */
    public void beainTransaction(){
    try {
    connectionUtils.getThreadConnection().setAutoCommit(false);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    /**
    * 提交事务
    */
    public void commit(){
    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().close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    @Around("pt()")
    public Object aroundAdvice(ProceedingJoinPoint pjp){
    Object rtValue=null;
    try {
    //1、获取参数
    Object[] args = pjp.getArgs();
    //2、开启事务
    this.beainTransaction();
    //3、执行方法
    rtValue=pjp.proceed(args);
    //4、提交事务
    this.commit();
    //5、返回结果
    return rtValue;
    } catch (Throwable e) {
    //6、回滚事务
    this.rollback();
    throw new RuntimeException("事务提交有误,请联系管理员");
    } finally {
    //7、释放资源
    this.release();
    }

    }

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

    <!--配置spring扫描包-->
    <context:component-scan base-package="com.hope"/>

    <!--配置QueryRunner-->
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
    </bean>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!--连接数据库的必备信息-->
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/easy"/>
    <property name="user" value="root"/>
    <property name="password" value="123"/>
    </bean>

    <!--开启spring对注解AOP的支持-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    </beans>


  1. }

AOP中环绕通知的书写和配置的更多相关文章

  1. AOP中环绕通知的写法

    package com.hope.utils;import org.aspectj.lang.ProceedingJoinPoint;/** * @author newcityman * @date ...

  2. java中AOP的环绕通知

    pom.xml <dependencies> <dependency> <groupId>org.springframework</groupId> & ...

  3. 在Spring aop中的propagation的7种配置的意思

    <tx:method name="find*" read-only="true" propagation ="NOT_SUPPORTED&quo ...

  4. spring aop中的propagation的7种配置的意思

    1.前言. 在声明式的事务处理中,要配置一个切面,即一组方法,如 <tx:advice id="txAdvice" transaction-manager="txM ...

  5. Spring AOP 中pointcut expression表达式解析及配置

    Pointcut是指那些方法需要被执行”AOP”,是由”Pointcut Expression”来描述的. Pointcut可以有下列方式来定义或者通过&& || 和!的方式进行组合. ...

  6. spring aop中的propagation的7种配置

    1.前言 在声明式的事务处理中,要配置一个切面,即一组方法,如 <tx:advice id="txAdvice" transaction-manager="txMa ...

  7. AOP 环绕通知 (Schema-base方式) 和 AspectJ方式在通知中获取切点的参数

    环绕通知(Schema- base方式) 1.把前置通知和后置通知都写到一个通知中,组成了环绕通知 2.实现步骤: 2.1 新建一个类实现 MethodInterceptor 接口 public cl ...

  8. spring中的AOP 以及各种通知 配置

    理解了前面动态代理对象的原理之后,其实还是有很多不足之处,因为如果在项目中有20多个类,每个类有100多个方法都需要判断是不是要开事务,那么方法调用那里会相当麻烦. spring中的AOP很好地解决了 ...

  9. 基于XML的AOP配置(2)-环绕通知

    配置方式: <aop:config> <aop:pointcut expression="execution(* com.itheima.service.impl.*.*( ...

随机推荐

  1. Python基础(序列化)

    #pickling import pickle,json # d = dict(name='傻狗1',age=300,score=100) # d1 = pickle.dumps(d)#pickle. ...

  2. Python基础(__slots__)

    class Point(object): __slots__ = ('name','point') p1 = Point() p1.name = 100 print(p1.name)#100 #p1. ...

  3. Nginx通过ngx_http_limit_req_module实现限制请求数、限速、白名单

    /etc/nginx/limit/white_list:白名单,key-value形式,支持掩码网段 #test 192.168.50.42 0; 192.168.50.0/24 0; /etc/ng ...

  4. [cf1290D]Coffee Varieties

    思路 统计数的种类数,也等价于统计有多少个数满足其之前没有与其相同的数 将序列以$\frac{k}{2}$为块大小分块,那么即会有$m=\frac{2n}{k}$个块 (关于$k=1$的情况,以1为块 ...

  5. negix启动不成功

    negix启动闪退,猜测可能端口占用,查看log发现 2020/11/30 11:38:40 [emerg] 15632#8688: CreateFile() "F:\项目工具\nginx- ...

  6. 你有没有觉得邮件发送人固定配置在yml文件中是不妥当的呢?SpringBoot 动态设置邮件发送人

    明月当天,不知道你有没有思念的人 前言 之前其实已经写过SpringBoot异步发送邮件,但是今天在一个小项目中要用到发送邮件时,我突然觉得邮件发送人只有一个,并且固定写在yml文件中,就是非常的不妥 ...

  7. Redis 持久化方案

    目录 持久化简介 什么是持久化? Redis 持久化方案 RDB RDB 简介 save 指令 操作与配置 工作原理 bgsave 指令 操作与配置 工作原理 bgsave 配置执行 相关配置 工作原 ...

  8. Codeforces 375C - Circling Round Treasures(状压 dp+最短路转移)

    题面传送门 注意到这题中宝藏 \(+\) 炸弹个数最多只有 \(8\) 个,故考虑状压,设 \(dp[x][y][S]\) 表示当前坐标为 \((x,y)\),有且仅有 \(S\) 当中的物品被包围在 ...

  9. Codeforces 1270E - Divide Points(构造+奇偶性)

    Codeforces 题目传送门 & 洛谷题目传送门 显然,直接暴力枚举是不可能的. 考虑将点按横纵坐标奇偶性分组,记 \(S_{i,j}=\{t|x_t\equiv i\pmod{2},y_ ...

  10. P6604 [HNOI2016]序列 加强版

    *I. P6604 [HNOI2016]序列 加强版 摘自学习笔记 简单树论 笛卡尔树部分例题 I. 和 P6503 比较类似.我们设 \(f_i\) 表示全局以 \(i\) 结尾的子区间的最小值之和 ...