Spring AOP variable value for method arguments around the board

Is it possible to change the value of a method argument based on some check before execution using Spring AOP

My method

public String doSomething(final String someText, final boolean doTask) { // Some Content return "Some Text"; } 

Consultation method

 public Object invoke(final MethodInvocation methodInvocation) throws Throwable { String methodName = methodInvocation.getMethod().getName(); Object[] arguments = methodInvocation.getArguments(); if (arguments.length >= 2) { if (arguments[0] instanceof String) { String content = (String) arguments[0]; if(content.equalsIgnoreCase("A")) { // Set my second argument as false } else { // Set my second argument as true } } } return methodInvocation.proceed(); } 

Please offer me a way to set the value of the method argument, since there are no settings for the argument.

+7
java spring spring-aop
source share
2 answers

I got my answer using MethodInvocation

 public Object invoke(final MethodInvocation methodInvocation) throws Throwable { String methodName = methodInvocation.getMethod().getName(); Object[] arguments = methodInvocation.getArguments(); if (arguments.length >= 2) { if (arguments[0] instanceof String) { String content = (String) arguments[0]; if(content.equalsIgnoreCase("A")) { if (methodInvocation instanceof ReflectiveMethodInvocation) { ReflectiveMethodInvocation invocation = (ReflectiveMethodInvocation) methodInvocation; arguments[1] = false; invocation.setArguments(arguments); } } else { if (methodInvocation instanceof ReflectiveMethodInvocation) { ReflectiveMethodInvocation invocation = (ReflectiveMethodInvocation) methodInvocation; arguments[1] = true; invocation.setArguments(arguments); } } } } return methodInvocation.proceed(); } 
+3
source share

Yes it is possible. You need ProceedingJoinPoint and instead:

 methodInvocation.proceed(); 

you can call the continue function with new arguments, for example:

 methodInvocation.proceed(new Object[] {content, false}); 

see http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/aop.html#aop-ataspectj-advice-proceeding-with-the-call

+6
source share

All Articles