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 Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2

springboot将lib和jar分离的操作方法

《springboot将lib和jar分离的操作方法》本文介绍了如何通过优化pom.xml配置来减小SpringBoot项目的jar包大小,主要通过使用spring-boot-maven-plugin... 遇到一个问题,就是每次maven package或者maven install后target中的ja

Java中八大包装类举例详解(通俗易懂)

《Java中八大包装类举例详解(通俗易懂)》:本文主要介绍Java中的包装类,包括它们的作用、特点、用途以及如何进行装箱和拆箱,包装类还提供了许多实用方法,如转换、获取基本类型值、比较和类型检测,... 目录一、包装类(Wrapper Class)1、简要介绍2、包装类特点3、包装类用途二、装箱和拆箱1、装

如何利用Java获取当天的开始和结束时间

《如何利用Java获取当天的开始和结束时间》:本文主要介绍如何使用Java8的LocalDate和LocalDateTime类获取指定日期的开始和结束时间,展示了如何通过这些类进行日期和时间的处... 目录前言1. Java日期时间API概述2. 获取当天的开始和结束时间代码解析运行结果3. 总结前言在J

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动