Spring Boot AOP 配置

背景

AOP(面向切面编程)是Spring的核心功能之一,本位对AOP的基本配置做一总结。

内容

  1. 切点表达式
execution([可见性] 返回类型 [方法所在类的完全限定名称].方法名(参数) [异常])
*:   匹配所有字符
..:  一般用于匹配多个包,多个参数
+:   表示类及其子类
  1. 切面注解
@Aspect     将一个java类定义为切面类
@Pointcut   定义一个切入点(可以是一个正则表达式,例如某个package下的所有函数,也可以是一个注解)

根据需要在切入点的不同位置切入内容:
@Before     在切入点开始时,切入内容
@After      在切入点结尾处,切入内容
@Around     在切入点前后处,切入内容,并自己控制何时执行内容的切入
@AfterReturning     在切入点return内容之后切入内容,用来定义对返回值的处理逻辑
@AfterThrowing      在切入点抛出异常后切入内容,用来定义抛出异常之后的处理逻辑
  1. 切面类(注意:需用@Componet标注后,才能被注册到Spring容器中)
@Aspect
@Component
public class MyAspect {

    private final String pointcut = "pointcut()";

    // 切点
    @Pointcut("execution(* com.rainlf.service.someService(..))")
    private void pointcut(){
    }

    // 前置通知
    @Before(pointcut)
    public void before(){
        System.out.println("前置通知....");
    }

    // 后置通知, returnVal: 切点方法执行后的返回值
    @AfterReturning(value=pointcut, returning = "returnVal")
    public void AfterReturning(Object returnVal){
        System.out.println("后置通知...." + returnVal);
    }

    // 环绕通知
    @Around(pointcut)
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取logger
        Class clazz = joinPoint.getTarget().getClass();
        Logger logger;
        try {
            Field field = clazz.getDeclaredField("logger");
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
            logger = (Logger) field.get(clazz);
        } catch (IllegalAccessException | NoSuchFieldException e) {
            logger = LoggerFactory.getLogger(clazz);
            logger.warn("目标类中无默认logger,建议添加。");
        }

        // 获取方法名
        String methodName = joinPoint.getSignature().getName();

        // 记录函数参数
        logger.debug("函数: {}, 参数: {}", methodName, Arrays.toString(joinPoint.getArgs()));

        // 函数开始时间
        long start = System.currentTimeMillis();

        // 执行函数
        Object obj;
        try {
            obj = joinPoint.proceed();
        } catch (Exception e) {
            logger.error("函数: {}, 发生异常: {}", methodName, e.getMessage());
            throw e;
        }

        // 函数结束时间
        long end = System.currentTimeMillis();

        // 记录函数返回值
        logger.debug("函数: {}, 返回值: {}", methodName, obj);

        // 记录函数耗时
        logger.debug("函数: {}, 耗时: {} ms", methodName, (end - start));

        return obj;
    }

    // 异常通知
    @AfterThrowing(value=pointcut, throwing = "e")
    public void afterThrowable(Throwable e) {
        System.out.println("出现异常:msg="+e.getMessage());
    }

    // 最终通知
    @After(value=pointcut)
    public void after(){
        System.out.println("最终通知....");
    }
}
  1. 切点通知顺序


  1. 切点定义相关规则图解


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • 本章内容: 面向切面编程的基本原理 通过POJO创建切面 使用@AspectJ注解 为AspectJ切面注入依赖 ...
    谢随安阅读 3,203评论 0 9
  • 本文是我自己在秋招复习时的读书笔记,整理的知识点,也是为了防止忘记,尊重劳动成果,转载注明出处哦!如果你也喜欢,那...
    波波波先森阅读 12,328评论 6 86
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,971评论 6 342
  • 玉女摇仙珮•济南漫游 徐 宏 趵突激湍天下无,珠玑万斛白玉壶。漱玉泉,水帘峡,红叶一谷四季明,珍珠泉中攒万珠。易...
    sunxuhong阅读 177评论 1 1