连接点
- 在Spring中用JoinPoint抽象了连接点,用它可以获取方法执行时的相关信息,如目标类名、方法名、方法参数等
- 对于@Around通知,获取连接点信息只能使用 ProceedingJoinPoint
- 对于其它四种通知,获取连接点信息只能使用JoinPoint,它时是ProceedingJoinPoint的父亲类型
- 具体代码如下
-
package com.example.tlias.AOP; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; 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.stereotype.Component; import java.util.Arrays; @Component @Aspect @Slf4j public class TestJoinPoint { @Pointcut("execution(* com.example.tlias.service.DeptLogService.*(..))") public void PointCut() { } @Before("PointCut()") public void before(JoinPoint joinPoint) { log.info("TestJointPoint...before..."); // 1.获取目标对象类名 String ClassName = joinPoint.getTarget().getClass().getName(); log.info("目标对象类名{}", ClassName); // 2.获取目标对象方法名 String Methodname = joinPoint.getSignature().getName(); log.info("目标对象的方法名{}", Methodname); // 3.获取目标方法运行时传入的参数 Object[] args = joinPoint.getArgs(); log.info("目标方法时传入的参数{}", Arrays.toString(args)); } @Around("PointCut()") public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { log.info("TestJointPoint...around..."); // 1.获取目标对象类名 String ClassName = proceedingJoinPoint.getTarget().getClass().getName(); log.info("目标对象的类名是{}", ClassName); // 2.获取目标对象方法名 String MethodName = proceedingJoinPoint.getSignature().getName(); log.info("目标对象的方法名{}", MethodName); // 3.获取目标方法运行时传入的参数 Object[] args = proceedingJoinPoint.getArgs(); log.info("目标方法运行时传入的参数{}", Arrays.toString(args)); // 4.放行目标方法执行 Object result = proceedingJoinPoint.proceed(); // 5.获取目标方法的返回值 log.info("目标方法运行的返回值{}", result); log.info("TestJointPoint around after...."); return result; } }
-