javaEEday12-AOP进阶-连接点

This commit is contained in:
xx
2026-11-07 15:26:13 +08:00
parent 6be8ba3b34
commit aff6337cdf
2 changed files with 60 additions and 1 deletions
@@ -8,7 +8,7 @@ import org.springframework.stereotype.Component;
//切面类
@Slf4j
@Aspect
//@Aspect
@Component
public class MyAspect6 {
@@ -0,0 +1,59 @@
package com.inmind.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;
//切面类
@Slf4j
@Aspect
@Component
public class MyAspect8 {
@Pointcut("execution(* com.inmind.service.DeptService.*(..))")
private void pt(){}
@Before("pt()")
public void before(JoinPoint joinPoint){
log.info("MyAspect8 ... before ...");
//1. 获取 目标对象的类名 .
String className = joinPoint.getTarget().getClass().getName();
log.info("before目标对象的类名:{}", className);
//2. 获取 目标方法的方法名 .
String methodName = joinPoint.getSignature().getName();
log.info("before目标方法的方法名: {}",methodName);
//3. 获取 目标方法运行时传入的参数 .
Object[] args = joinPoint.getArgs();
log.info("before目标方法运行时传入的参数: {}", Arrays.toString(args));
}
@Around("pt()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("MyAspect8 around before ...");
//1. 获取 目标对象的类名 .
String className = joinPoint.getTarget().getClass().getName();
log.info("around目标对象的类名:{}", className);
//2. 获取 目标方法的方法名 .
String methodName = joinPoint.getSignature().getName();
log.info("around目标方法的方法名: {}",methodName);
//3. 获取 目标方法运行时传入的参数 .
Object[] args = joinPoint.getArgs();
log.info("around目标方法运行时传入的参数: {}", Arrays.toString(args));
//4. 放行 目标方法执行并返回结果 .其他4个通知是获取不到返回值的,JoinPoint没有提供该方法,
Object result = joinPoint.proceed();
//5. 获取 目标方法运行的返回值 .
log.info("around目标方法运行的返回值: {}",result);
return result;//注意:null默认是不返回,导致有返回值的方法失效,此处代码修改为return result;
}
}