springboot 切面AOP

2024-08-21 11:36

本文主要是介绍springboot 切面AOP,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

切面AOP的实现

1、包引入

在默认的包中没有,需要单独集成

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId><version>3.3.2</version><!-- 版本根据自己的springboot选择,这里使用的版本为3.3.2-->
</dependency>

springboot默认有的starter,对应的版本在spring-boot-dependencies-3.3.2.pom中定义默认依赖版本
在这里插入图片描述

2、切面定义流程

2.1 切面定义流程

1、在类上使用@Aspect 声明该类为一个切面类,同时注意使用@Component将切面注入到容器中
2、在切面类方法使用@Pointcut注解声明一个切点方法,该方法为一个空的方法体,该方法不执行任何具体业务逻辑,主要用于标记或作为切入点表达式的依据。
切点表达式常用为两种 execution() 和 @annotation,

execution(modifier? ret-type declaring-type?name-pattern(param-pattern) throws-pattern?)

所有表达式方法

  • arg () 限制连接点的指定参数为指定类型的执行方法
  • @args () 限制连接点匹配参数由指定注解标注的执行方法
  • execution () 用于匹配连接点的执行方法
  • this () 限制连接点匹配 AOP 代理的 Bean 引用为指定类型的类
  • target () 限制连接点匹配特定的执行对象,这些对象对应的类要具备指定类型注解
  • within() 限制连接点匹配指定类型
  • @within() 限制连接点匹配指定注释所标注的类型
  • @annotation 限制匹配带有指定注释的连接点

3、在切面类中通过注解声明切面的通知方法(也就是不同执行时机的回调方法) Spring 切面可应用的 5 种通知类型:

  • @Before——在方法调用之前调用通知
  • @After——在方法完成之后调用通知,无论方法执行成功与否
  • @After-returning——在方法执行成功之后调用通知
  • @After-throwing——在方法抛出异常后进行通知
  • @Around——通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为

在定义通知方法的时候,一般可以使用 JoinPoint 作为参数,环绕通知使用 ProceedingJoinPoint。

完整代码

切面类
package org.javatrip.springbootaop;import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;@Aspect
@Component
@Slf4j
public class PerformanceAspect {@Pointcut("execution(* org.javatrip.springbootaop.*.*(..))")public void pointCutExecution(){}@Before("pointCutExecution()")public void beforePointCutExecution() {log.info("切面====before_pointCut_execution====");}@AfterReturning("pointCutExecution()")public void afterReturningPointCutExecution() {log.info("切面====after_returning_pointCut_execution====");}@AfterThrowing("pointCutExecution()")public void afterThrowingPointCutExecution() {log.info("切面====after_throwing_pointCut_execution====");}@After("pointCutExecution()")public void afterPointCutExecution() {log.info("切面====after_pointCut_execution====");}@Around("pointCutExecution()")public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("=====aroundBefore method execution...");try {Object result = joinPoint.proceed();System.out.println("=====aroundAfter method execution...");return result;} catch (Throwable t) {System.out.println("====Exception in method execution: " + t.getMessage());throw t;}}
}
package org.javatrip.springbootaop;import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
@Slf4j
public class AOPController {@GetMapping(value = "/testAOP/{id}")@PermissionAnnotation(permissionName = "userlist", permissionType = "get")public String testAop(@PathVariable("id") String id) {log.info("====Controller方法===={}",id);
//        throw new RuntimeException("test");return "test====Controller方法===="+id;}
}

正常情况下执行顺序
通知==== aroundBefore method execution====
通知==== before_pointCut_execution====
==== Controller方法====
通知 ==== after_returning_pointCut_execution====
通知==== after_pointCut_execution====
通知===== aroundAfter method execution====

异常情况下执行顺序
通知==== aroundBefore method execution…
通知==== before_pointCut_execution====
==== Controller方法====
通知==== after_throwing_pointCut_execution====
通知==== after_pointCut_execution====

2.2 切点表达式

切点表达式组合
使用 &&、|| 和 ! 来组合多个切点表达式,表示多个表达式“与”、“或”和“非”的逻辑关系。
这可以用来组合多种类型的表达式,来提升匹配效率。
@Before(“pointCutExecution() && args(account,…)”)

2.3 其他表达式

任何在com.xyz.service包中的方法
within(com.xyz.service.*)
任何定义在com.xyz.service包或者其子包中的方法
within(com.xyz.service..*)
任何实现了com.xyz.service.AccountService接口中的方法
this(com.xyz.service.AccountService)
任何目标对象实现了com.xyz.service.AccountService的方法
target(com.xyz.service.AccountService)
一般情况下代理类(Proxy)和目标类(Target)都实现了相同的接口,所以上面的2个基本是等效的。有且只有一个Serializable参数的方法
args(java.io.Serializable)
只要这个参数实现了java.io.Serializable接口就可以,不管是java.io.Serializable还是Integer,还是String都可以。目标(target)使用了@Transactional注解的方法
@target(org.springframework.transaction.annotation.Transactional)
目标类(target)如果有Transactional注解的所有方法
@within(org.springframework.transaction.annotation.Transactional)
任何方法有Transactional注解的方法
@annotation(org.springframework.transaction.annotation.Transactional)
有且仅有一个参数并且参数上类型上有Transactional注解
@args(org.springframework.transaction.annotation.Transactional) 	注意是参数类型上有Transactional注解,而不是方法的参数上有注解。bean的名字为tradeService中的方法
bean(simpleSay) 	bean名字为simpleSay中的所有方法。bean名字能匹配
bean(*Impl) 	bean名字匹配*Impl的bean中的所有方法。

3、多个切面之间的顺序

Spring AOP 中一个目标类或方法可以被多个切面切入,可以使用 @Order(数字) 注解来指定切面之间的优先级,来控制切面的执行顺序。Order 值越小,优先级越大。
在这里插入图片描述

4、切点配置 注解配置

在Spring Boot应用中,是否需要显式添加@EnableAspectJAutoProxy注解来启用AOP支持,取决于你的项目配置和使用的Spring Boot版本。默认情况在Spring Boot环境中,由于存在spring-boot-autoconfigure依赖,它默认会注入AopAutoConfiguration配置类,该类的作用等同于@EnableAspectJAutoProxy注解。这意味着在大多数情况下,不需要显式添加@EnableAspectJAutoProxy注解。

这篇关于springboot 切面AOP的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1093042

相关文章

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.