AOP获取参数名称

由于项目中打印日志的需要,研究了一下在aop中,获取参数名称的方法。

1、jdk1,8中,比较简单,直接通过joinPoint中的getSignature()方法即可获取

[java]  view plain  copy
  1. Signature signature = joinpoint.getSignature();  
  2. MethodSignature methodSignature = (MethodSignature) signature;  
  3. String[] strings = methodSignature.getParameterNames();  
  4. System.out.println(Arrays.toString(strings));  



2.通用方法。比较麻烦

[java]  view plain  copy
  1. public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable{  
  2.           
  3.         String classType = joinPoint.getTarget().getClass().getName();    
  4.         Class<?> clazz = Class.forName(classType);    
  5.         String clazzName = clazz.getName();    
  6.         String methodName = joinPoint.getSignature().getName(); //获取方法名称   
  7.         Object[] args = joinPoint.getArgs();//参数  
  8.           //获取参数名称和值  
  9.         Map<String,Object > nameAndArgs = getFieldsName(this.getClass(), clazzName, methodName,args);   
  10.         System.out.println(nameAndArgs.toString());  
  11.         //为了省事,其他代码就不写了,  
  12.         return result = joinPoint.proceed();  
  13.    
  14. }  

[java]  view plain  copy
  1. private Map<String,Object> getFieldsName(Class cls, String clazzName, String methodName, Object[] args) throws NotFoundException {   
  2.         Map<String,Object > map=new HashMap<String,Object>();  
  3.           
  4.         ClassPool pool = ClassPool.getDefault();    
  5.         //ClassClassPath classPath = new ClassClassPath(this.getClass());    
  6.         ClassClassPath classPath = new ClassClassPath(cls);    
  7.         pool.insertClassPath(classPath);    
  8.             
  9.         CtClass cc = pool.get(clazzName);    
  10.         CtMethod cm = cc.getDeclaredMethod(methodName);    
  11.         MethodInfo methodInfo = cm.getMethodInfo();  
  12.         CodeAttribute codeAttribute = methodInfo.getCodeAttribute();    
  13.         LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);    
  14.         if (attr == null) {    
  15.             // exception    
  16.         }    
  17.        // String[] paramNames = new String[cm.getParameterTypes().length];    
  18.         int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;    
  19.         for (int i = 0; i < cm.getParameterTypes().length; i++){    
  20.             map.put( attr.variableName(i + pos),args[i]);//paramNames即参数名    
  21.         }    
  22.           
  23.         //Map<>  
  24.         return map;    
  25.     } 
### Spring AOP 后置通知中获取方法参数 在Spring AOP框架内,通过`AfterReturningAdvice`或带有`@AfterReturning`注解的方法能够实现后置通知的功能。当目标方法成功完成后触发此类通知,在此过程中如果想要获取被拦截方法的参数,则可以通过引入`JoinPoint`作为参数之一来达成目的[^1]。 ```java import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; @Aspect public class MethodParamLoggingAspect { @AfterReturning(pointcut = "execution(* com.example.service.*Service.*(..))", returning = "retVal") public void logMethodParams(JoinPoint joinPoint, Object retVal) { // 获取方法签 String methodName = joinPoint.getSignature().getName(); // 获取方法参数名称和值 Object[] args = joinPoint.getArgs(); System.out.println("Method Name: " + methodName); System.out.print("Arguments: "); for (Object arg : args) { System.out.print(arg + ", "); } System.out.println("\nReturn Value: " + retVal); } } ``` 上述代码展示了如何利用`JoinPoint`对象中的`getArgs()`方法取得实际传入到业务逻辑层服务类方法里的各个参数,并打印出来以便于日志记录或其他处理需求[^2]。 值得注意的是,虽然这里展示的例子是基于控制台输出的方式来进行参数查看,但在真实项目开发环境中更常见做法可能是把这些信息保存至数据库或是发送给监控平台用于后续分析[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值