Spring之AOP

AOP术语

  • 通知 Advice : 切面的工作。通知定义了切面是什么,以及何时使用。
    • 前置通知 Before
    • 后置通知 After : 无论是否抛出异常都会调用
    • 返回通知 After-returing :方法正常执行无异常之后调用
    • 异常通知 After-throwing : 在目标方法抛出异常后调用
    • 环绕通知 Around : 通知包裹了被通知的方法, 在通知的方法调用之前、之后、抛出异常时执行自定义行为
  • 连接点 JoinPoint:可以插入切面的点,是可以应用通知的时机,如方法调用时、抛出异常时
  • 切点 Poincut: 如果通知定义了“什么”和“何时”,那切点定义了“何处”。切点的定义会匹配通知所要织入的一个或多个连接点。
  • 切面 Aspect: 切面时通知和切点的结合。通知和切点共同定义了切面的全部内容,它是什么,再何时何处完成其功能。
  • 引入 Introduction: 引入允许我们向现有的类添加新的方法或属性。
  • 织入 Weaving: 织入是把切面应用到目标对象并创建新的代理对象的过程。

编写切点

execution(访问修饰符  返回值  包名.包名.包名...类名.方法名(参数列表))

XML约束

<?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/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

XML配置AOP

    <aop:config>
        <aop:pointcut id="transaction" expression="execution(* com.ttpfx.service.impl.*.*(..))"/>
        <aop:aspect ref="transactionManager">
<!--            <aop:before method="beginTransaction" pointcut-ref="transaction"/>-->
<!--            <aop:after-returning method="commit" pointcut-ref="transaction"/>-->
<!--            <aop:after-throwing method="rollback" pointcut-ref="transaction"/>-->
<!--            <aop:after method="release" pointcut-ref="transaction"/>-->
            <aop:around method="around" pointcut-ref="transaction"/>
        </aop:aspect>
    </aop:config>

注解配置AOP

XML中开启注解AOP的支持

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

注解:

  • @Aspect 声明当前类为切面类
  • @Pointcut("execution(....)") 声明切点
  • @Before("pointcut()")
  • @After("pointcut()")
  • @AfterReturning("pointcut()")
  • @AfterThrowing("pointcut()")
  • @Around("pointcut()")
package com.ttpfx.utils;


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

@Component("transactionManager")
@Aspect
public class TransactionManager {
    private ConnectionUtils connectionUtils;

    @Autowired
    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    @Pointcut("execution(* com.ttpfx.service.impl.*.*(..))")
    public void pointcut() {}

    @Before("pointcut()")
    public void beginTransaction() {
        System.out.println("begin");
        try {
            connectionUtils.getConnection().setAutoCommit(false);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @AfterReturning("pointcut()")
    public void commit() {
        System.out.println("commit");
        try {
            connectionUtils.getConnection().commit();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @AfterThrowing("pointcut()")
    public void rollback() {
        System.out.println("roolback");
        try {
            connectionUtils.getConnection().rollback();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @After("pointcut()")
    public void release() {
        System.out.println("release");
        try {
            connectionUtils.getConnection().close();
            connectionUtils.removeConnection();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

环绕通知

    public Object around(ProceedingJoinPoint jp) {
        try {
            Object[] args = jp.getArgs();
            beginTransaction();
            Object ret = jp.proceed(args);
            commit();
            return ret;
        } catch (Throwable throwable) {
            rollback();
            throw new RuntimeException(throwable);
        } finally {
            release();
        }
    }
posted @ 2021-03-25 17:40  ttpfx  阅读(62)  评论(0)    收藏  举报