Get method arguments using Spring AOP?

后端 未结 8 2009
陌清茗
陌清茗 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:47

    you can get method parameter and its value and if annotated with a annotation with following code:

    Map annotatedParameterValue = getAnnotatedParameterValue(MethodSignature.class.cast(jp.getSignature()).getMethod(), jp.getArgs()); ....

    private Map getAnnotatedParameterValue(Method method, Object[] args) {
            Map annotatedParameters = new HashMap<>();
            Annotation[][] parameterAnnotations = method.getParameterAnnotations();
            Parameter[] parameters = method.getParameters();
    
            int i = 0;
            for (Annotation[] annotations : parameterAnnotations) {
                Object arg = args[i];
                String name = parameters[i++].getDeclaringExecutable().getName();
                for (Annotation annotation : annotations) {
                    if (annotation instanceof AuditExpose) {
                        annotatedParameters.put(name, arg);
                    }
                }
            }
            return annotatedParameters;
        }
    

提交回复
热议问题