[Spring]-AOP

发布于:2025-06-09 ⋅ 阅读:(17) ⋅ 点赞:(0)

AOP场景

AOP: Aspect Oriented Programming (面向切面编程)

OOP: Object Oriented Programming (面向对象编程)

场景设计

  1. 设计: 编写一个计算器接口和实现类,提供加减乘除四则运算

  1. 需求: 在加减乘除运算的时候需要记录操作日志(运算前参数、运算后结果)
  2. 实现方案:
  • 硬编码
  • 静态代理
  • 动态代理
  • AOP

硬编码

使用硬编码的方式记录日志

  1. 创建工程环境: 新建模块, 加一个lombok依赖

  1. 新建计算器接口和实现类, 并在实现类中, 通过硬编码记录日志
package com.guigu.aop.calculator;

// 定义四则运算
public interface MathCalculator {

    // 加法
    int add(int i, int b);

    // 减法
    int sub(int i, int b);

    // 乘法
    int mul(int i , int b);

    // 除法
    int div(int i, int b);
}
package com.guigu.aop.calculator.impl;


/**
 * 计算器实现类
 * 1. 硬编码: 不推荐; 耦合:(通用逻辑 + 专用逻辑)希望不要耦合; 耦合太多就是维护地狱
 */
@Component
public class MathCalculatorImpl implements MathCalculator {
    @Override
    public int add(int i, int b) {
        System.out.println("[日志] add开始, 参数:" + i + "," + b);
        int res = i + b;
        System.out.println("[日志] add结束, 结果:" + res);
        return res;
    }

    @Override
    public int sub(int i, int b) {
        return i -b;
    }

    @Override
    public int mul(int i, int b) {
        return i * b;
    }

    @Override
    public int div(int i, int b) {
        return  i / b;
    }
}
  1. 新建单元测试, 测试一下
package com.guigu.aop;

@SpringBootTest
public class MathTest {

    @Autowired
    MathCalculatorImpl mathCalculator;

    @Test
    void test01() {
        int add = mathCalculator.add(1, 9);
    }
}

静态代理

编码时介入: 包装真实对象,对外提供静态代理对象

实现步骤

  1. 包装被代理对象
  2. 实现被代理对象的接口
  3. 运行时调用被代理对象的真实方法
  4. 外部使用代理对象调用

优点

  • 实现简单

缺点

  • 需要为不同类型编写不同代理类,导致扩展维护性差

使用静态代理技术, 记录代码日志

package com.guigu.aop.proxy.statics;

import com.guigu.aop.calculator.MathCalculator;
import lombok.Data;
import org.springframework.stereotype.Component;

/**
 * 静态代理: 定义代理对象, 帮助目标对象完成一些工作
 */
@Component
@Data
public class CalculatorStaticProxy implements MathCalculator {

    private MathCalculator target; // 目标对象

    public CalculatorStaticProxy(MathCalculator mc) {
        this.target = mc;
    }

    @Override
    public int add(int i, int b) {
        System.out.println("[日志] add开始, 参数:" + i + "," + b);
        int res = target.add(i, b);
        System.out.println("[日志] add结束, 结果:" + res);
        return res;
    }

    @Override
    public int sub(int i, int b) {
        return target.sub(i, b);
    }

    @Override
    public int mul(int i, int b) {
        return target.mul(i, b);
    }

    @Override
    public int div(int i, int b) {
        return target.div(i, b);
    }
}
package com.guigu.aop;


@SpringBootTest
public class MathTest {

    @Autowired
    CalculatorStaticProxy calculatorStaticProxy;

    @Test
    void test02() {

        int add = calculatorStaticProxy.add(2, 3);
    }
}

动态代理

运行时介入: 创建真实对象运行时代理对象

  1. 实现步骤
  • ·Java 反射提供 Proxy.newProxyInstance 的方式创建代理对象
  1. 优点:
  • 节约不同代理类的开发
  1. 缺点:
  • 开发难度大
  • 必须有接口,才能创建动态代理

使用动态代理技术, 记录代码日志

  1. 封装一个日志工具类, 提供记录日志的静态方法
package com.guigu.aop.log;


public class LogUtils {
    public static void logStart(String name, Object... args) {
        System.out.println("[日志]: [" + name + "]开始, 参数:" + Arrays.toString(args));
    }

    public static void logEnd(String name) {
        System.out.println("[日志]: [" + name + "]结束");
    }

    public static void logException(String name, Throwable e) {
        System.out.println("[日志]: [" +name+ "]异常: 异常信息:" + e.getCause());
    }

    public static void logReturn(String name, Object result) {
        System.out.println("[日志]: [" + name + "]结束, 返回结果:" + result);
    }
}
  1. 创建动态代理类 封装一个静态方法, 可以传入任何对象, 返回代理对象
package com.guigu.aop.proxy.dynamic;

/**
 * 动态代理
 * 1.是java原生支持的技术
 * 2.运行期间才决定代理关系, 类似于拦截器的思想
 * 3.目标对象在执行期间会被动态拦截, 可以插入指定逻辑
 * 4.优点: 可以代理任何对象
 * 5.缺点: 代码多
 * 6.强制要求: 目标对象必须有接口(否则报错), 代理的也只是接口规定的方法,
 */
@Component
public class DynamicProxy {
    // 获取目标对象的代理对象
    public static Object getProxyInstance(Object target) {

        /**
         * Proxy是java反射包提供的类
         * 下面的方法可以创建对象的代理对象, 需要三个参数
         * Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
         * 参数1: ClassLoader loader, 类加载器 (通过类加载器拿到目标对象)
         * 参数2: Class<?>[] interfaces, 目标对象实现的接口 (通过接口拿到目标对象实现的方法)
         * 参数3: InvocationHandler h, 代理对象需要执行的方法, 这个方法中可以插入指定逻辑
         */

        /**
         * 拦截方法说明:
         * 通过拦截方法, 可以拦截到目标对象方法的调用, 从而做任何事情
         *  (proxy, method, args)-> { }
         *  proxy: 代理对象
         *  method: 准备执行的目标对象的方法
         *  args: 方法调用传递的参数
         */

        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                (proxy, method, args)-> {
                    String name = method.getName();
                    // 记录开始
                    LogUtils.logStart(name, args);
                    Object result = null;
                    try {
                        result = method.invoke(target, args); // 执行目标对象的方法
                        // 记录返回值
                        LogUtils.logReturn(name, result);
                    } catch (Exception e) {
                        // 记录异常
                        LogUtils.logException(name, e);
                    } finally {
                        // 记录结束
                        LogUtils.logEnd(name);
                    }

                    return result;
                });
    }
}
  1. 使用动态代理类的方法创建代理对象
package com.guigu.aop;


@SpringBootTest
public class MathTest {

    @Autowired
    MathCalculatorImpl mathCalculator;

    @Autowired
    DynamicProxy dynamicProxy;

    @Test
    void test03() {
        MathCalculator instance =(  MathCalculator) dynamicProxy.getProxyInstance(mathCalculator);
        instance.add(3,5);
    }
}

AOP使用

了解AOP的术语

AOP基本使用步骤

  1. 导入AOP依赖
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
  1. 编写切面Aspect
package com.guigu.aop.aspect;

@Component
@Aspect // 告诉spring这个组件是一个切面
public class LogAspect {

}
  1. 编写通知方法
package com.guigu.aop.aspect;

@Component
@Aspect // 告诉spring这个组件是一个切面
public class LogAspect {

    public void logStart() {
        System.out.println("[切面-日志]开始...");
    }

    public void logEnd() {
        System.out.println("[切面-日志]结束...");
    }

    public void logReturn() {
        System.out.println("[切面-日志]返回结果");
    }

    public void logException() {
        System.out.println("[切面-日志]爬出抛出异常:");
    }
}
  1. 指定切入点表达式
package com.guigu.aop.aspect;

import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect // 告诉spring这个组件是一个切面
public class LogAspect {

    /**
     * 告诉Spring, 以下通知何时何地生效?
     * 何时?
     *   通知方法:
     *   @Before: 方法执行前运行
     *   @AfterReturning: 方法执行正常返回结果运行
     *   @AfterThrowing: 方法抛出异常时运行
     *   @After: 方法执行之后运行
     *  何地:
     *    切入点表达式:
     *       1. execution(方法的全签名)
     *       作用: 根据方法匹配切入点
     *       全写法: [public] int [com.guigu.aop.calculator].add(int, int) [throws ArithmeticException]
     *       省略写法: int add(int, int)
     *       通配符:
     *          *: 表示任意字符
     *         ..: 表示多个参数, 任意类型
     *       最省略: * *(..)
     */

    @Before("execution(int com.guigu.aop.calculator.MathCalculator.*(..))")
    public void logStart() {
        System.out.println("[切面-日志]开始...");
    }

    @After("execution(int com.guigu.aop.calculator.MathCalculator.*(..))")
    public void logEnd() {
        System.out.println("[切面-日志]结束...");
    }

    @AfterReturning("execution(int com.guigu.aop.calculator.MathCalculator.*(..))")
    public void logReturn() {
        System.out.println("[切面-日志]返回结果");
    }

    @AfterThrowing("execution(int com.guigu.aop.calculator.MathCalculator.*(..))")
    public void logException() {
        System.out.println("[切面-日志]爬出抛出异常:");
    }
}
  1. 测试AOP动态织入
package com.guigu.aop;

import com.guigu.aop.calculator.MathCalculator;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class AopTest {

    @Autowired // 容器中注入的是  MathCalculator 的代理对象
    MathCalculator mathCalculator;

    @Test
    void test01() {

        System.out.println(mathCalculator.getClass()); // 看看代理对象
        mathCalculator.add(10, 20);

    }
}

AOP细节

切入点表达式

切入点表达式的写法

package com.guigu.aop.annotation;

import java.lang.annotation.*;

// 自定义接口
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAn {
}
package com.guigu.aop.calculator.impl;

import com.guigu.aop.annotation.MyAn;
import com.guigu.aop.calculator.MathCalculator;
import org.springframework.stereotype.Component;

/**
 * 计算器实现类
 * 1. 硬编码: 不推荐; 耦合:(通用逻辑 + 专用逻辑)希望不要耦合; 耦合太多就是维护地狱
 */
@Component
public class MathCalculatorImpl implements MathCalculator {
    @Override
    public int add(int i, int b) {
        int res = i + b;
        return res;
    }

    @Override
    public int sub(int i, int b) {
        return i -b;
    }

    @Override
    public int mul(int i, int b) {
        return i * b;
    }

    @MyAn
    @Override
    public int div(int i, int b) {
        return  i / b;
    }
}
package com.guigu.aop.aspect;

import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect // 告诉spring这个组件是一个切面
public class LogAspect {

    /**
     * 告诉Spring, 以下通知何时何地生效?
     * 何时?
     *   @Before: 方法执行前运行
     *   @AfterReturning: 方法执行正常返回结果运行
     *   @AfterThrowing: 方法抛出异常时运行
     *   @After: 方法执行之后运行
     *  何地:
     *    切入点表达式:
     *       1. execution(方法的全签名)
     *       作用: 根据方法匹配切入点
     *       全写法: [public] int [com.guigu.aop.calculator].add(int, int) [throws ArithmeticException]
     *       省略写法: int add(int, int)
     *       通配符:
     *          *: 表示任意字符
     *         ..: 表示多个参数, 任意类型
     *       最省略: * *(..)
     *       2. args(参数类型或其子类型)
     *       作用: 根据方法的参数匹配切入点
     *       3. annotation(注解类型)
     *       作用: 根据方法的注解匹配切入点, 一般配合自定义注解使用
     */


    @Before("args(int, int)")
    public void logHaha() {
        System.out.println("[切面-日志]哈哈...");
    }

    @Before("@annotation(com.guigu.aop.annotation.MyAn)")
    public void logHehe() {
        System.out.println("[切面-日志]呵呵...");
    }
}
package com.guigu.aop;

@SpringBootTest
public class AopTest {

    @Autowired // 容器中注入的是  MathCalculator 的代理对象
    MathCalculator mathCalculator;


    @Test
    void test02() {

        // 根据方法的参数匹配切入点
        mathCalculator.add(1, 2);

        // 测试方法的注解匹配切入点
        mathCalculator.div(1, 2);

    }
}

执行顺序

AOP 的底层原理

1、Spring会为每个被切面切入的组件创建代理对象(Spring CGLIB 创建的代理对象,无视接口)。

2、代理对象中保存了切面类里面所有通知方法构成的增强器链。

3、目标方法执行时,会先去执行增强器链中拿到需要提前执行的通知方法去执行

通知方法的执行顺序

  1. 正常链路: 前置通知->目标方法->返回通知->后置通知
  2. 异常链路: 前置通知->目标方法->异常通知->后置通知

连接点信息

通过 JoinPoint 包装了当前目标方法的所有信息

通过 returning 属性可以接收当前方法的返回值

通过 throwing 属性可以接收当前方法的异常信息

package com.guigu.aop.aspect;

@Component
@Aspect // 告诉spring这个组件是一个切面
public class LogAspect {

    @Before("execution(int com.guigu.aop.calculator.MathCalculator.*(..))")
    public void logStart(JoinPoint joinPoint) {

        // 拿到方法全签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取方法名
        String name = signature.getName();
        // 获取方法的参数值
        Object[] args = joinPoint.getArgs();

        System.out.println("【切面- 日志】【" + name + "】开始:参数列表:【" + Arrays.toString(args) + "】");
    }

    @After("execution(int com.guigu.aop.calculator.MathCalculator.*(..))")
    public void logEnd(JoinPoint joinPoint) {

        // 拿到方法全签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取方法名
        String name = signature.getName();
        System.out.println("[切面-日志] " + name + "结束...");
    }

    @AfterReturning(
            value = "execution(int com.guigu.aop.calculator.MathCalculator.*(..))",
            returning = "result"
    )
    public void logReturn(JoinPoint joinPoint, Object result) {

        // 拿到方法全签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取方法名
        String name = signature.getName();

        System.out.println("【切面 -日志】【" + name + "】返回:值:" + result);
    }

    @AfterThrowing(
            value = "execution(int com.guigu.aop.calculator.MathCalculator.*(..))",
            throwing = "e"
    )
    public void logException(JoinPoint joinPoint, Exception e) {
        // 拿到方法全签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取方法名
        String name = signature.getName();

        System.out.println("【切面 - 日志】【" + name + "】异常:错误信息:【" + e.getMessage() + "】");
    }

}

抽取切入点表达式

使用@Pointcut 注解抽取切入点表达式

package com.guigu.aop.aspect;

@Component
@Aspect // 告诉spring这个组件是一个切面
public class LogAspect {

    @Pointcut("execution(int com.guigu.aop.calculator.MathCalculator.*(..))")
    public void pointCut(){};

    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint) {

        // 拿到方法全签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取方法名
        String name = signature.getName();
        // 获取方法的参数值
        Object[] args = joinPoint.getArgs();

        System.out.println("【切面- 日志】【" + name + "】开始:参数列表:【" + Arrays.toString(args) + "】");
    }

    @After("pointCut()")
    public void logEnd(JoinPoint joinPoint) {

        // 拿到方法全签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取方法名
        String name = signature.getName();
        System.out.println("[切面-日志] " + name + "结束...");
    }

    @AfterReturning(
            value = "pointCut()",
            returning = "result"
    )
    public void logReturn(JoinPoint joinPoint, Object result) {

        // 拿到方法全签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取方法名
        String name = signature.getName();

        System.out.println("【切面 -日志】【" + name + "】返回:值:" + result);
    }

    @AfterThrowing(
            value = "pointCut()",
            throwing = "e"
    )
    public void logException(JoinPoint joinPoint, Exception e) {
        // 拿到方法全签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取方法名
        String name = signature.getName();

        System.out.println("【切面 - 日志】【" + name + "】异常:错误信息:【" + e.getMessage() + "】");
    }

}

多切面的执行顺序

默认情况下, 切面方法的执行顺序受切面类的首字母排序影响

通过 Order 注解可以指定切面类的优先级

package com.guigu.aop.aspect;

@Order(1) // 数值越小, 优先级越高, 执行越早
@Component
@Aspect
public class LogAspect {

    @Pointcut("execution(int com.guigu.aop.calculator.MathCalculator.*(..))")
    public void pointCut(){};

    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint) {

        // 拿到方法全签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取方法名
        String name = signature.getName();
        // 获取方法的参数值
        Object[] args = joinPoint.getArgs();

        System.out.println("【切面- 日志】【" + name + "】开始:参数列表:【" + Arrays.toString(args) + "】");
    }

    @After("pointCut()")
    public void logEnd(JoinPoint joinPoint) {

        // 拿到方法全签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取方法名
        String name = signature.getName();
        System.out.println("[切面-日志] " + name + "结束...");
    }

    @AfterReturning(
            value = "pointCut()",
            returning = "result"
    )
    public void logReturn(JoinPoint joinPoint, Object result) {

        // 拿到方法全签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取方法名
        String name = signature.getName();

        System.out.println("【切面 -日志】【" + name + "】返回:值:" + result);
    }

    @AfterThrowing(
            value = "pointCut()",
            throwing = "e"
    )
    public void logException(JoinPoint joinPoint, Exception e) {
        // 拿到方法全签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取方法名
        String name = signature.getName();

        System.out.println("【切面 - 日志】【" + name + "】异常:错误信息:【" + e.getMessage() + "】");
    }
}
package com.guigu.aop.aspect;

@Component
@Aspect
public class AuthAspect {

    @Pointcut("execution(int com.guigu.aop.calculator.MathCalculator.*(..))")
    public void pointCut(){};

    @Before("pointCut()")
    public void authStart() {
        System.out.println("[切面-权限] 开始");
    }

    @After("pointCut()")
    public void authEnd() {
        System.out.println("[切面-权限] 结束");
    }

    @AfterReturning("pointCut()")
    public void authReturn() {
        System.out.println("【切面 -权限】 返回");
    }

    @AfterThrowing("pointCut()")
    public void authException() {
        System.out.println("【切面 - 权限】 异常");
    }


}
package com.guigu.aop;

@SpringBootTest
public class AopTest {

    @Test
    void test04() {

        mathCalculator.add(1, 2);

    }
}

环绕通知

环绕通知可以控制目标方法是否执行, 修改目标方法的参数和执行结果

package com.guigu.aop.aspect;

@Aspect
@Component
public class AroundAspect {
    /**
     * 环绕通知的固定写法如下
     * Object: 返回值
     * ProceedingJoinPoint: 可以继续推进的切入点
     */

    @Pointcut("execution(int com.guigu.aop.calculator.MathCalculator.*(..))")
    public void pointCut(){};

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        // 获取目标方法的参数
        Object[] args = pjp.getArgs();

        System.out.println("[切面-环绕前置]: 参数" + Arrays.toString(args));

        Object proceed = null;
        try {
            // 继续执行目标方法
            proceed = pjp.proceed(args);
            System.out.println("[切面-环绕返回]: 返回值" + proceed);
        } catch (Throwable e) {
            System.out.println("[切面-环绕异常]: 异常信息" + e.getMessage());
            throw e; // 抛出异常, 让别人继续感知, 否则异常会被吃掉, 影响后面的程序
        } finally {
            System.out.println("[切面-环绕后置]");
        }

        // 目标方法执行完毕,返回结果
        return proceed;
    }
}
package com.guigu.aop;

@SpringBootTest
public class AopTest {

    @Autowired // 容器中注入的是  MathCalculator 的代理对象
    MathCalculator mathCalculator;

    @Test
    void test04() {

        mathCalculator.add(1, 2);

    }
}


网站公告

今日签到

点亮在社区的每一天
去签到