SpringBoot AOP中JoinPoint的用法和通知切点表达式

2024-06-16 14:38

本文主要是介绍SpringBoot AOP中JoinPoint的用法和通知切点表达式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

上一篇文章讲解了springboot aop 初步完整的使用和整合 这一篇讲解他的接口方法和类

JoinPoint和ProceedingJoinPoint对象

  1. JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoint对象.

  2. ProceedingJoinPoint对象是JoinPoint的子接口,该对象只用在@Around的切面方法中

@Aspect
@Component
public class aopAspect {/*** 定义一个切入点表达式,用来确定哪些类需要代理* execution(* aopdemo.*.*(..))代表aopdemo包下所有类的所有方法都会被代理*/@Pointcut("execution(* aopdemo.*.*(..))")public void declareJoinPointerExpression() {}/*** 前置方法,在目标方法执行前执行* @param joinPoint 封装了代理方法信息的对象,若用不到则可以忽略不写*/@Before("declareJoinPointerExpression()")public void beforeMethod(JoinPoint joinPoint){System.out.println("目标方法名为:" + joinPoint.getSignature().getName());System.out.println("目标方法所属类的简单类名:" +        joinPoint.getSignature().getDeclaringType().getSimpleName());System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName());System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));//获取传入目标方法的参数Object[] args = joinPoint.getArgs();for (int i = 0; i < args.length; i++) {System.out.println("第" + (i+1) + "个参数为:" + args[i]);}System.out.println("被代理的对象:" + joinPoint.getTarget());System.out.println("代理对象自己:" + joinPoint.getThis());}/*** 环绕方法,可自定义目标方法执行的时机* @param pjd JoinPoint的子接口,添加了*            Object proceed() throws Throwable 执行目标方法*            Object proceed(Object[] var1) throws Throwable 传入的新的参数去执行目标方法*            两个方法* @return 此方法需要返回值,返回值视为目标方法的返回值*/@Around("declareJoinPointerExpression()")public Object aroundMethod(ProceedingJoinPoint pjd){Object result = null;try {//前置通知System.out.println("目标方法执行前...");//执行目标方法//result = pjd.proeed();//用新的参数值执行目标方法result = pjd.proceed(new Object[]{"newSpring","newAop"});//返回通知System.out.println("目标方法返回结果后...");} catch (Throwable e) {//异常通知System.out.println("执行目标方法异常后...");throw new RuntimeException(e);}//后置通知System.out.println("目标方法执行后...");return result;}
}

切点表达式

  1. 在Spring AOP中,连接点始终代表方法的执行。切入点是与连接点匹配的,切入点表达语言是以编程方式描述切入点的方式。

  2. 切入点(Poincut)是定义了在“什么地方”进行切入,哪些连接点会得到通知。显然,切点一定是连接点

  3. 切点是通过@Pointcut注解和切点表达式定义的。@Pointcut注解可以在一个切面内定义可重用的切点。

execute表达式

*代表匹配任意修饰符及任意返回值,参数列表中..匹配任意数量的参数

可以使用&&、||、!、三种运算符来组合切点表达式,表示与或非的关系

  1. 拦截任意公共方法execution(public * *(..))
  2. 拦截以set开头的任意方法execution(* set*(..))
  3. 拦截类或者接口中的方法
拦截AccountService(类、接口)中定义的所有方法
execution(* com.xyz.service.AccountService.*(..))
  1. 拦截包中定义的方法,不包含子包中的方法
拦截com.xyz.service包中所有类中任意方法,**不包含**子包中的类
execution(* com.xyz.service.*.*(..))
  1. 拦截包或者子包中定义的方法
拦截com.xyz.service包或者子包中定义的所有方法
execution(* com.xyz.service..*.*(..))

通知分类

@Before

  1. 前置通知: 在方法执行之前执行
  2. 前置通知使用@Before注解 将切入点表达式值作为注解的值

@After

  1. 后置通知, 在方法执行之后执行
  2. 后置通知使用@After注解 ,在后置通知中,不能访问目标方法执行的结果

@AfterRunning

  1. 返回通知, 在方法返回结果之后执行
  2. 返回通知使用@AfterRunning注解

@AfterThrowing

  1. 异常通知, 在方法抛出异常之后执行
  2. 异常通知使用@AfterThrowing注解

@Around

  1. 环绕通知, 围绕着方法执行
  2. 环绕通知使用@Around注解

package com.jason.spring.aop.impl;import java.util.Arrays;
import java.util.List;import org.aspectj.lang.JoinPoint;
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.springframework.stereotype.Component;//把这个类声明为一个切面
//1.需要将该类放入到IOC 容器中
@Component
//2.再声明为一个切面
@Aspect
public class LoggingAspect {//声明该方法是一个前置通知:在目标方法开始之前执行 哪些类,哪些方法//作用:@before 当调用目标方法,而目标方法与注解声明的方法相匹配的时候,aop框架会自动的为那个方法所在的类生成一个代理对象,在目标方法执行之前,执行注解的方法//支持通配符//@Before("execution(public int com.jason.spring.aop.impl.ArithmeticCaculatorImpl.*(int, int))")@Before("execution(* com.jason.spring.aop.impl.*.*(int, int))")public void beforeMethod(JoinPoint joinPoint){String methodName = joinPoint.getSignature().getName();List<Object> args = Arrays.asList(joinPoint.getArgs());System.out.println("The method " + methodName + " begins " + args);}/*** @Description:  在方法执行后执行的代码,无论该方法是否出现异常* @param joinPoint*/@After("execution(* com.jason.spring.aop.impl.*.*(int, int))")public void afterMethod(JoinPoint joinPoint){String methodName = joinPoint.getSignature().getName();List<Object> args = Arrays.asList(joinPoint.getArgs());System.out.println("The method " + methodName + " end " + args);}/*** * @Description:  在方法正常结束后执行代码,放回通知是可以访问到方法的返回值** @param joinPoint*/@AfterReturning( value="execution(* com.jason.spring.aop.impl.*.*(..))", returning="result")public void afterReturning(JoinPoint joinPoint ,Object result){String methodName = joinPoint.getSignature().getName();System.out.println("The method " + methodName + " end with " + result);}/*** * @Description:  在目标方法出现异常时会执行代码,可以访问到异常对象,且,可以指定出现特定异常时执行通知代码** @param joinPoint* @param ex*/@AfterThrowing(value="execution(* com.jason.spring.aop.impl.*.*(..))",throwing="ex")public void afterThrowting(JoinPoint joinPoint, Exception  ex){String methodName = joinPoint.getSignature().getName();System.out.println("The method " + methodName + " occurs exceptions " + ex);}/*** * @Description: 环绕通知需要携带 ProceedingJoinPoint 类型的参数*                    环绕通知 类似于  动态代理的全过程*                   ProceedingJoinPoint:可以决定是否执行目标方法*    环绕通知必须有返回值,返回值即为目标方法的返回值*    * @param proceedingJoinPoint*/@Around("execution(* com.jason.spring.aop.impl.*.*(..))")public Object around(ProceedingJoinPoint proceedingJoinPoint){Object result = null;String methodName = proceedingJoinPoint.getSignature().getName();//执行目标方法try {//前置通知System.out.println("The method " + methodName + "begin with" + Arrays.asList(proceedingJoinPoint.getArgs()));result = proceedingJoinPoint.proceed();//后置通知System.out.println("The method " + methodName + "end with" + result);} catch (Throwable e) {//异常通知System.out.println("The method occurs exception : " + e);throw new RuntimeException();}//后置通知System.out.println("The method " + methodName + "end with" + result);return result;        }
}

切点表达式参考

这篇关于SpringBoot AOP中JoinPoint的用法和通知切点表达式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

C语言中%zu的用法解读

《C语言中%zu的用法解读》size_t是无符号整数类型,用于表示对象大小或内存操作结果,%zu是C99标准中专为size_t设计的printf占位符,避免因类型不匹配导致错误,使用%u或%d可能引发... 目录size_t 类型与 %zu 占位符%zu 的用途替代占位符的风险兼容性说明其他相关占位符验证示

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick