当前位置: 首页 > news >正文

【Spring框架】Spring AOP

目录

  • 什么是AOP?
  • AOP组成
  • Spring AOP 实现步骤
  • Spring AOP实现原理
    • JDK Proxy VS CGLIB

什么是AOP?

AOP(Aspect Oriented Programming):⾯向切⾯编程,它是⼀种思想,它是对某⼀类事情的集中处理。⽐如⽤户登录权限的效验,没学 AOP 之前,我们所有需要判断⽤户登录的⻚⾯(中的⽅法),都要各⾃实现或调⽤⽤户验证的⽅法,然⽽有了 AOP 之后,我们只需要在某⼀处配置⼀下,所有需要判断⽤户登录⻚⾯(中的⽅法)就全部可以实现⽤户登录验证了,不再需要每个⽅法中都写相同的⽤户登录验证了。

AOP组成

  1. 切面(Aspect):定义的是事件(AOP是啥的)。ex:用户登录校验
  2. 切点(Pointcut):定义具体规则。ex:定义用户登录拦截规则,哪些接口判断用户登录权限?哪些不判断。
  3. 通知(Advice):AOP执行的具体方法。ex:获取用户登录信息,如果获取到说明已经登录,否则未登录。
    前置通知
    后置通知
    环绕通知
    返回通知
  4. 连接点(Join Point):有可能触发切点的所有点。ex:所有接口

Spring AOP 实现步骤

1.添加Spring AOP依赖

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-bo
ot-starter-aop -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2.定义切面。

package com.example.demo.common;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Aspect // 定义切面
@Component
public class UserAspect {  
}

3.定义切点。

package com.example.demo.common;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Aspect // 定义切面
@Component
public class UserAspect {// 切点@Pointcut("execution(* com.example.demo.controller.UserController.*(..))")public void pointcut() {}
}

4.执行通知。

package com.example.demo.common;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Aspect // 定义切面
@Component
public class UserAspect {// 切点@Pointcut("execution(* com.example.demo.controller.UserController.*(..))")public void pointcut() {}// 通知@Before("pointcut()")public void doBefore() {System.out.println("执行了前置通知");}
}
package com.example.demo.common;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;@Aspect // 定义切面
@Component
public class UserAspect {// 切点@Pointcut("execution(* com.example.demo.controller.UserController.*(..))")public void pointcut() {}// 通知@Before("pointcut()")public void doBefore() {System.out.println("执行了前置通知");}// 后置通知@After("pointcut()")public void doAfter() {System.out.println("执行了后置方法");}// 环绕通知@Around("pointcut()")public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("环绕通知执行之前");// 执行目标方法Object result = joinPoint.proceed();System.out.println("环绕通知执行之后");return result;}
}

在这里插入图片描述

Spring AOP实现原理

Spring AOP 是构建在动态代理基础上,因此 Spring 对 AOP 的⽀持局限于⽅法级别的拦截。
Spring 动态代理组成:
1.JDK Proxy 代理对象必须实现接口,才能使用JDK Proxy 。
2.CGLIB 通过实现代理类的子类来实现动态代理。

JDK Proxy VS CGLIB

1.出生不同。
2.实现不同:JDK Proxy要求代理类实现接口才能实现代理; CGLIB 是通过实现子类完成动态代理。
3.性能不同:JDK 7+ JDK Proxy性能是略高于CGLIB ;JDK 7之前 CGLIB 性能远远高于JDK Proxy。

http://www.lryc.cn/news/107051.html

相关文章:

  • 寻找旋转排序数组中的最小值——力扣153
  • 安卓逆向 - 基础入门教程
  • 验证码安全志:AIGC+集成环境信息信息检测
  • R-Meta分析教程
  • 【3维视觉】3D空间常用算法(点到直线距离、面法线、二面角)
  • Nodejs 第四章(Npm install 原理)
  • [深度学习] GPU处理能力(TFLOPS/TOPS)
  • js:获取浏览器默认语言
  • 【U8+】用友U8重新注册加密锁,提示:写卡失败,请重新配置客户端控件。
  • uniapp小程序console.log在微信开发者工具中不打印问题
  • 从零基础开始开发自己的第一个微信小程序
  • 无涯教程-Lua - Arrays(数组)
  • 0基础学习VR全景平台篇 第76篇:全景相机-圆周率全景相机如何直播推流
  • 超详细|ChatGPT论文润色教程
  • MMDeploy安装、python API测试及C++推理
  • [openCV]基于拟合中线的智能车巡线方案V3
  • vite+typescript项目 :找不到模块“./***.vue”或其相应的类型声明——解决方案
  • Gradio-YOLOv5-YOLOv7 搭建Web GUI
  • HTML模板生成word,pdf文档
  • ssl单向证书和双向证书校验测试及搭建流程
  • 【2种方法,jmeter用一个正则提取器提取多个值!】
  • 012-堆,结构体
  • GDAL C++ API 学习之路 OGRGeometry 多边形类 OGRPolygon
  • 文件传输协议FTP与托管文件传输MFT有什么区别?
  • js实现按照句号将一段文本进行分段
  • 环形链表的进一步探究
  • flink任务性能优化
  • vue2 el-carousel轮播图和文字一起改变
  • LangChain:打造自己的LLM应用 | 京东云技术团队
  • 字节跳动测试岗,3面都过了,HR告诉我这个原因被刷了...