Spring Aop How to Get Parameter Names and Parameter Values

  • 2021-11-01 03:27:41
  • OfStack

Foreword:

Sometimes when we are programming with Spring Aop, we need to get the parameter name and parameter value of the connection point (JoinPoint) method.

Environment:

Mac OSX Intellij IDEA Spring Boot 2x Jdk 1.8x

Code:


package com.example.aopdemo.aop; 
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.CodeSignature;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * DemoAop
 * Create by Gray(Ganguocai@outlook.com)
 */
@Aspect
@Component
@Slf4j
public class DemoAop {
 
    /**
     *  Surround notification 
     * @param proceedingJoinPoint
     * @return
     * @throws Throwable
     */
    @Around(value = "execution(* com.example.aopdemo..*(..)))")
    public Object demoAop(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        
        log.debug(" Before implementation: ");
        
        Map<String, Object> params = getNameAndValue(proceedingJoinPoint);
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            System.out.println("name: " + entry.getKey() + " value: " + entry.getValue());
        }
        
        Object object = proceedingJoinPoint.proceed();  // Execute the join point method, object Method return value 
        
        log.debug(" After implementation: ");
        
        return object;
    }
    /**
     *  Get parameters Map Set 
     * @param joinPoint
     * @return
     */
    Map<String, Object> getNameAndValue(ProceedingJoinPoint joinPoint) {
        Map<String, Object> param = new HashMap<>();
        Object[] paramValues = joinPoint.getArgs();
        String[] paramNames = ((CodeSignature)joinPoint.getSignature()).getParameterNames();
        for (int i = 0; i < paramNames.length; i++) {
            param.put(paramNames[i], paramValues[i]);
        }
        return param;
    }
}

A Tip for Obtaining Parameters of AOP Section

1 Generally speaking, our parameters are passed through json, so this problem turns into a problem of getting the specified string from json.

OK, the problem is simple.

As follows:


public static void main(String[] args) {
    //  Here JSONObject Yes fastjson-1.2.41.jar Under the bag 
    JSONObject jsonObject = JSON.parseObject("{\"timeStamp\":21602756894612,\"status\":0,\"results\":{\"userName\":\"yang20102\",\"userLevel\":\"3\"},\"errorCode\":null,\"errorMessage\":null}");
    //  Get json Outermost string 
    Object timeStamp = jsonObject.get("timeStamp");
    System.out.println("timeStamp:" + timeStamp);
 
    //  Get complex objects 
    Object results = jsonObject.get("results");
    JSONObject jsonObjectResults = JSON.parseObject(results.toString());
    Object userName = jsonObjectResults.get("userName");
    System.out.println("userName:" + userName);
}

Example json is as follows:


{
  "timeStamp": 21602756894612,
  "status": 0,
  "results": {
    "userName": "yang20102",
    "userLevel": "3"
  },
  "errorCode": null,
  "errorMessage": null
}

Related articles: