Get method arguments using Spring AOP?

后端 未结 8 1994
陌清茗
陌清茗 2020-11-29 01:13

I am using Spring AOP and have below aspect:

@Aspect
public class LoggingAspect {

    @Before(\"execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..         


        
8条回答
  •  一整个雨季
    2020-11-29 01:51

    if your using @Aspect an option is add this method inside your Aspect and send the JoinPoint and the name of parameter you need.

    private Object getParameter(ProceedingJoinPoint joinPoint, String parameterName) {
        Object valueParameter = null;
        if (Objects.nonNull(joinPoint) && joinPoint.getSignature() instanceof MethodSignature
                && Objects.nonNull(parameterName) ) {
            MethodSignature method = (MethodSignature)joinPoint.getSignature();
            String[] parameters = method.getParameterNames();
            for (int t = 0; t< parameters.length; t++) {
                if( Objects.nonNull(parameters[t]) && parameters[t].equals(parameterName)) {
                    Object[] obj = joinPoint.getArgs();
                    valueParameter = obj[t];
                }
            }
        }
        return valueParameter;
    }
    

    and the call example:

    Object parameterObject = getParameter(joinPoint, "nameClient");
    if ( Objects.nonNull(parameterObject) ) {
        String parametro = String.valueOf(parameterObject);
    }
    

    Only need know the type of object for convert

提交回复
热议问题