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

SpringAOP常用功能实现

1. 导入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2. 核心通知

package com.example.aspect;import lombok.SneakyThrows;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;@Aspect
@Order(1)
@Component
public class RequestAspect {@Pointcut("execution(* com.example.controller..*(..))")public void pc() {}/*** around通知必须有返回值*/@SneakyThrows@Around("com.example.aspect.RequestAspect.pc()")public Object around(ProceedingJoinPoint joinPoint) {System.out.println("around start");Object result = joinPoint.proceed();System.out.println("around end");return result;}@Before("pc()")public void before() {System.out.println("before");}@After("pc()")public void after() {System.out.println("after");}@AfterReturning("pc()")public void afterReturning() {System.out.println("afterReturning");}@AfterThrowing("pc()")public void afterThrowing() {System.out.println("afterThrowing");}
}
around start
before
@RequestMapping
afterReturning
after
around end

3. 执行顺序

@Around joinPoint.proceed()

@Before @RequestMapping @AfterReturning/@AfterThrowing @After

@Around

4. 切点表达式

4.1 execution方法签名匹配

返回类型和异常可以省略。

execution(<访问修饰符> <返回类型> <包名.类名.⽅法(⽅法参数)> <异常>)
@Pointcut("execution(* com.example.controller..*(..))")

4.2 @annotation注解匹配

@annotation(注解路径)
@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")

4.3 @Pointcut切点表达式

@Pointcut可以定义公共切点表达式,也可以在其他切面类中使用。

@Around("com.example.aspect.RequestAspect.pc()")

 

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

相关文章:

  • Java基础的重点知识-04-封装
  • win7 的 vmware tools 安装失败
  • 【杂记-浅谈OSPF协议之IR、ABR、ASBR、BR】
  • element 问题整合
  • Linux下vi文件的时候替换指定的内容
  • 【知识学习】阐述Unity3D中MaterialTexture的概念及使用方法示例
  • java创建0byte的空文件
  • Qt 实战(6)事件 | 6.2、事件过滤器
  • 【PyTorch】【机器学习】图片张量、通道分解合成和裁剪
  • 如何提高工业交换机的电源功耗?
  • 源站静态文件更新后,CDN会自动刷新吗
  • Token的应用场景
  • 机器学习课程复习——奇异值分解
  • Java--乐观锁
  • 静默升级oracle 11g (从11.2.0.1升级到11.2.0.4)
  • 什么是模型训练,如何选择合适的Batch大小
  • 【线上绘图网站分享】
  • Snipaste截图工具如何控制框线箭头的粗细程度
  • GISSERVER 管理器 1.0(私有化地图离线部署)
  • Eureka服务治理深度解析:服务下线与剔除机制揭秘
  • 苹果笔记本双系统怎么安装
  • 探索网络爬虫技术:原理、实践与挑战
  • GitHub国内使用方法
  • Java调用第三方HTTP接口的常用方式
  • DOPE-PEG2000-FITC荧光特性
  • 华为Pura70支持5G功能吗?看完你就清楚了
  • android 4大组件用法
  • qt pro工程文件通用宏定义
  • 这次让我们隆重的介绍一下
  • 大语言模型系列-Transformer