SpringBoot使用注解开发AOP

我不是女神ヾ 2023-10-04 22:32 14阅读 0赞

SpringBoot使用注解开发AOP

  1. AOP是一种对面向对象编程的补充,它的全称是面向切面编程(Aspect Oriented Programming)。在面向对象编程中,主要关注的是类的结构和组织方式。而在AOP中,主要关注的是某种行为跨越多个类和层的情况,并使用切面来描述这种行为并将其应用到多个目标上。
  2. AOP的核心概念有两个: 切面和连接点。切面描述了跨越多个类和层的行为,它包含了一组连接点。连接点是应用程序中执行某些操作的点,这些操作被称为建议。建议可以在连接点之前、之后或之间插入。
  3. AOP的主要用途是在系统运行时跨越多个类和层应用特定行为,例如日志、事务管理、安全性等。通过使用AOP,可以将跨越多个类和层的行为抽象出来,并使其可重用和可维护。
  4. Aop:
  5. 把一个个横切点关注点放到某一个模块中去,称之为切点。那么每一个切面都能影响业务的某一种功能。切点的目的就是功能的增强,比如日志切面,就是一个横切关注点。应用中许多方法需要做日志记录,只需要插入预制的切面即可。

在这里插入图片描述

1、AOP术语:

  1. 1JoinPoint (连接点):被拦截到需要增强的方法。即 where:去哪里做增强操作
  2. 2. Pointcut (切入点):哪些包中的哪些类中的哪些方法做增强?可能是连接点的集合。where:去那些地方做增强
  3. // JoinPoint的集合
  4. 3Advice: 当拦截到 JoinPoint 之后,在方法执行什么时机(when)做什么样(what)的增强。
  5. 根据时机分为:
  6. 1)前置增强
  7. 2)后置增强
  8. 3)异常增强
  9. 4)最终增强
  10. 5)环绕增强
  11. 4AsPect (切面): 何地 何时 做什么
  12. Pointcut+Advice ====> 去哪些地方 + 在什么时候 + 做什么增强?
  13. 5Target: 目标对象,被代理的目标对象
  14. 6weaving: 织入、把Advice加到 Target 上之后 创建出 Proxy 对象的进程
  15. 7Poxy : 一个类被Aop织入增强后产生的代理类

Advice 执行时机:

  1. Public void dowork(){
  2. <------------------------------------前置增强
  3. try{
  4. //业务代码
  5. <------------------------------------后置增强
  6. }catch{
  7. // 异常处理
  8. <------------------------------------异常增强
  9. }finally{
  10. //释放资源
  11. <------------------------------------最终增强
  12. }
  13. }

2、Aop注解:

  1. @Before: 在切点方法之前执行
  2. @After:在切点方法之后执行
  3. @AfterReturning: 切点方法返回后执行
  4. @AfterThrowing: 切点方法抛异常执行
  5. @Around: 属于环绕增强,能控制切点执行前,执行后,用这个注解后,程序抛异常,会影响@AfterThrowing这个注解

#

3、例:自定义注解结合Aop

步骤:

定义要被增强的业务类:
  1. package cn.js.Controller;
  2. import cn.js.proxy.ApiPermission;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. /**
  7. * @BelongsProject: SpringBootAop
  8. * @Author: com.js
  9. * @CreateTime: 2023-03-02 16:32
  10. * @Version: 1.0
  11. */
  12. @RestController
  13. @RequestMapping("/get")
  14. public class StudentController {
  15. @GetMapping("/student")
  16. @ApiPermission //通过自定义的注解告诉SpringBoot这里作为切面
  17. public void Studnt(){
  18. System.out.println("执行修改操作");
  19. }
  20. }
自定义注解
  1. package cn.js.proxy;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. import java.lang.annotation.Target;
  6. /**
  7. *@description: 定义一个注解,作为切入点
  8. **/
  9. @Target(ElementType.METHOD)
  10. @Retention(RetentionPolicy.RUNTIME)
  11. public @interface ApiPermission {
  12. }
定义一个切面类:
  1. package cn.js.proxy;
  2. import org.aspectj.lang.annotation.After;
  3. import org.aspectj.lang.annotation.Aspect;
  4. import org.aspectj.lang.annotation.Before;
  5. import org.aspectj.lang.annotation.Pointcut;
  6. import org.springframework.stereotype.Component;
  7. /**
  8. * @BelongsProject: 定义一个切面类
  9. * @Author: com.js
  10. * @CreateTime: 2023-03-02 16:35
  11. * @Version: 1.0
  12. */
  13. @Component
  14. @Aspect
  15. public class AspectPermissit {
  16. /**
  17. *@description: 告诉aop我们是通过注解,cn.js.proxy下面的注解作为一个切入点,只要打上这个注解就要对其贴上注解方法做增强
  18. 通过我们的注解作为一个切入点
  19. **/
  20. @Pointcut("@annotation(cn.js.proxy.ApiPermission)")
  21. public void texpoint(){
  22. }
  23. /**
  24. *@description: 比方说在我们这个texpoint 方法执行之前切入
  25. **/
  26. @Before("texpoint()")
  27. public void begin(){
  28. System.out.println("通过session或者redis,获取我们的用户信息");
  29. System.out.println("通过tokn解析出用户ID");
  30. System.out.println("再去查询是否有权限");
  31. }
  32. /**
  33. *@description: 比方说在我们这个texpoint 方法执行之后切入
  34. **/
  35. @After("texpoint()")
  36. public void login(){
  37. System.out.println("-------操作成功后执行------");
  38. System.out.println("提交事务!!!!");
  39. }
  40. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,14人围观)

还没有评论,来说两句吧...

相关阅读