Spring Boot 集成 AOP 教程

学习知识点

  • 什么是切面编程,什么是切入点?
  • 切入点能做什么?实际应用?
  • AOP的各个通知的实际用途?
  • 通过Spring Boot 集成 AOP 使用?

项目结构图

准备工作

  • 常用IDE,这里是IDEA 2020.3
  • JDK1.8
  • Maven 3.5 +

AOP 介绍

通常我们的APP有接口控制层业务逻辑层数据交互层,各自负责的模块不同,但是也有很多共通点,例如日志权限

日常项目使用:

  • 日志:统一记录,不用麻烦重复的每个方法都记录一个操作人,代码冗余很多
  • 权限:使用aop灵活的校验一些特殊的方法权限
  • 性能:记录每个方法执行的时间,耗时久的发出短信/邮件通知等

什么是通知,连接点或切入点?

  1. 连接点是程序的执行点,例如方法的执行或异常的处理,在Spring AOP中,连接点始终意味着方法的执行。
  2. 切点是与连接点匹配的表达式。
  3. 通知 与切入点表达式关联,并在与该切入点匹配的连接点处运行

AOP通知的类型

在这里插入图片描述

SpringBoot AOP 示例

先定义一个切面

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Aspect
@Configuration
public class TestAspectJ {

    // 定义拦截通知
}

在启动类开启 AOP 代理

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class SpringbootAopApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootAopApplication.class, args);
    }

}

前置通知

    // 前置通知
    @Before("execution(* com.github.gleans.springbootaop.controller..*(..))")
    public void before(JoinPoint joinPoint){
        // 通知
        log.info("前置通知测试");
        log.info(" 当前节点:{}", joinPoint);
    }

里面的表达式,..表示当前包及子包,第一个*表示返回值的类型任意

定义一个接口,来测试前置通知

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("test")
@RestController
public class TestController {

    @GetMapping("/before")
    public String before() {
        log.info("请求 test/before 接口");
        return "前置通知测试";
    }
}

请求接口:http://127.0.0.1:8080/test/before
看日志

环绕通知

下面我们来实战,写一个计算方法耗时的AOP通知

    // 环绕通知
    @Around("execution(* com.github.gleans.springbootaop.controller..*(..))")
    public Object handlerControllerMethod(ProceedingJoinPoint joinPoint) {
        long startTime = System.currentTimeMillis();
        Object res = null;
        try {
            res = joinPoint.proceed();
        } catch (Throwable throwable) {
            log.error("出错了:{}", throwable.getLocalizedMessage());
        }

        log.info(joinPoint.getSignature() + "=耗时:=" + (System.currentTimeMillis() - startTime));

        return res;
    }

效果

正常返回通知

增强处理是在目标方法正常请求后被织入

    //也可以定义切入点
    @Pointcut(value = "execution(* com.github.gleans.springbootaop.controller..*(..))")
    public void testAfterReturing(){};

    // 正常返回通知
    @AfterReturning(value = "testAfterReturing()", returning = "res")
    public Object afterReturning(String res) {
        // 通知
        log.info("正常返回通知测试,返回值:{}", res);
        return "222";
    }

结果

异常返回通知

    @AfterThrowing(value = "testAfterReturing()", throwing="ex")
    public void AfterThrowing(JoinPoint jp, Throwable ex) {
        log.info("=====异常返回通知====:{}", ex.getLocalizedMessage());
    }

后置通知

    // 后置通知
    @After("execution(* com.github.gleans.springbootaop.controller..*(..))")
    public void after(JoinPoint joinPoint) {
        // 通知
        log.info("后置通知测试,当前节点:{}", joinPoint);
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员鱼丸

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

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

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

打赏作者

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

抵扣说明:

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

余额充值