浅谈Spring中的JoinPoint MethodSignature Signature

浅谈Spring中的JoinPoint MethodSignature Signature

AOP 中,切面可以通过 JoinPoint 对象获取到连接点的相关信息,其中包括方法签名信息。

  1. 通过 JoinPoint 对象的 getSignature() 方法获取到 Signature 对象。
  2. 如果连接点是一个方法,那么可以将 Signature 对象转换为 MethodSignature 对象,通过 MethodSignature 对象可以获取到更加详细的方法签名信息,比如方法返回类型、参数类型等。

JoinPoint

JointPoint是程序运行过程中可识别的点,这个点可以用来作为AOP切入点。

JointPoint对象则包含了和切入相关的很多信息。比如切入点的对象,方法,属性等。我们可以通过反射 的方式获取这些点的状态和信息。

Spring AOP 中的 JoinPoint 接口继承了反射 API 中的 Member 接口,因此可以通过 JoinPoint 对象获取到方法 或构造函数的 Signature 对象。

ProceedingJoinPoint

ProceedingJoinPoint 是 JoinPoint 的子接口,专门用于环绕通知(@Around)。

多了一个proceed() 方法,proceed() 方法是环绕通知中的关键,它决定了是否继续执行原方法或者提前返回自定义结果

MethodSignature 切入点是一个方法

[😒: 代码实现============>](#😒: 代码实现============>)

✒️ 用MethodSignature的方法签名对象,可以获取方法的详细信息,例如方法名、参数列表等,以便进行切面的逻辑处理。

getName() 方法用于获取方法名

getReturnType() 方法用于获取方法返回类型

getParameterTypes() 方法用于获取方法参数类型列表等

📘代码

java 复制代码
@Aspect
@Component
@Slf4j
public class MyAspect {
	
     // 【切入点 对哪些类 哪些方法来进行拦截】
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut(){}
    
    // 【前置通知】
    @Before("autoFillPointCut()")
    public void beforeAdvice(JoinPoint joinPoint) {
        // 获取方法签名对象
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();

        // 获取方法名
        String methodName = methodSignature.getName();

        // 获取参数类型数组
        Class[] parameterTypes = methodSignature.getParameterTypes();

        // 获取返回类型
        Class returnType = methodSignature.getReturnType();

		//获得方法上的注解对象
		AutoFill autoFill = methodSignature.getMethod().getAnnotation(AutoFill.class);

        // 打印方法信息
        System.out.println("方法名:" + methodName);
        System.out.println("参数类型:" + Arrays.toString(parameterTypes));
        System.out.println("返回类型:" + returnType.getName());
    }
}

Signature

✒️

📘代码

java 复制代码
@Aspect
@Component
public class MyAspect {

    @Before("execution(* com.example.service.*.*(..))")
    public void beforeAdvice(JoinPoint joinPoint) {
        // 获取连接点的签名信息
        Signature signature = joinPoint.getSignature();

        // 获取方法名
        String methodName = signature.getName();

        // 获取参数类型数组
        Class[] parameterTypes = signature.getParameterTypes();

        // 获取返回类型
        Class returnType = signature.getReturnType();

        // 打印方法信息
        System.out.println("方法名:" + methodName);
        System.out.println("参数类型:" + Arrays.toString(parameterTypes));
        System.out.println("返回类型:" + returnType.getName());
    }
}

           
    
相关推荐
孫治AllenSun1 分钟前
【线程池】优化等待队列和拒绝策略
java·spring boot·spring cloud
毕设源码-邱学长27 分钟前
【开题答辩全过程】以 基于Spring Boot的体育场地预约管理系统为例,包含答辩的问题和答案
java·spring boot·后端
青槿吖43 分钟前
第二篇:告别XML臃肿配置!Spring注解式IOC/DI保姆级教程,从入门到真香
xml·java·开发语言·数据库·后端·sql·spring
t198751281 小时前
TOA定位算法MATLAB实现(二维三维场景)
开发语言·算法·matlab
梦想的旅途21 小时前
如何通过 QiWe API 实现企业微信主动发消息
开发语言·python
jllllyuz1 小时前
粒子群算法解决资源分配问题的MATLAB实现
开发语言·算法·matlab
凌晨一点的秃头猪1 小时前
Python文件操作
开发语言·python
摇滚侠1 小时前
讲一讲 SpringMVC,线程变量 ThreadLocal 的使用
java·spring boot·intellij-idea
myloveasuka2 小时前
C++进阶:利用作用域解析运算符 :: 突破多态与变量隐藏
开发语言·c++
小哇6662 小时前
第2篇:Spring Boot + WebSocket + 消息队列STOMP协议(Rabbitmq) 架构原理
后端·websocket