java Spring AOP 更改方法参数的值围绕建议
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32369685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring AOP change value of methods argument on around advice
提问by Ashish Aggarwal
Is it possible to change method argument value on basis of some check before executing using Spring AOP
是否可以在使用 Spring AOP 执行之前根据一些检查更改方法参数值
My method
我的方法
public String doSomething(final String someText, final boolean doTask) {
// Some Content
return "Some Text";
}
Advice 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 suggest me the way to set the method argument value as there is no setter options for the argument.
请建议我设置方法参数值的方法,因为该参数没有设置器选项。
采纳答案by Ashish Aggarwal
I got my answer using MethodInvocation
我得到了我的答案 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();
}
回答by reto
Yes that's possible. You need a ProceedingJoinPoint
and instead of:
是的,这是可能的。你需要一个ProceedingJoinPoint
and 而不是:
methodInvocation.proceed();
you can then call proceed with the new arguments, for example:
然后,您可以使用新参数调用继续,例如:
methodInvocation.proceed(new Object[] {content, false});
回答by Subin Chalil
You can use Spring AOP, create point cut using @Around
.
Then you can use the below code to change the arguments of the method, based on the condition.
您可以使用 Spring AOP,使用@Around
. 然后您可以使用以下代码根据条件更改方法的参数。
int index = 0;
Object[] modifiedArgs = proceedingJoinPoint.getArgs();
for (Object arg : proceedingJoinPoint.getArgs()) {
if (arg instanceof User) { // Check on what basis argument have to be modified.
modifiedArgs[index]=user;
}
index++;
}
return proceedingJoinPoint.proceed(modifiedArgs); //Continue with the method with modified arguments.