[英]Using AspectJ to set the value of an annotated method parameter
I imagine a method 我想办法
public void fooMethod(Object param1, @SetupParam Object param2){
// ... do stuff
}
That I want to call without the need of having to set param2 我想调用而无需设置param2
fooMethod("param1");
but having it magically setup in an advise. 但是在建议中神奇地设置它。 I am currently working with this code (simplified)
我目前正在使用此代码(简体)
@Around("@annotation(com.example.SetupParam)")
public Object setupParam(ProceedingJoinPoint pjp) throws Throwable {
Object[] args = pjp.getArgs();
args[1] = "setup";
return pjp.proceed(args);
}
but I need to call that method every time with 但是我每次都要用
fooMethod("param1", null);
So I want to get rid of the null parameter. 所以我想摆脱null参数。 I am not using @Autowiring since the second parameter inside the method is always instantiated with different values.
我没有使用@Autowiring,因为方法内的第二个参数始终使用不同的值实例化。
I think Your pointcut definiition is wrong. 我认为您的切入点定义是错误的。 What You need is
@args
definition with annotation inside. 您需要的是
@args
定义,其中带有注释。 This matches methods whose arguments are annotated with defined annotation (as You described in question) your advice should look similar (haven't tested it) to 这与参数用定义的注释(如您所描述的注释)注释的方法匹配,您的建议应类似于(未经测试)
@Around("@args(.., com.example.SetupParam))")
note that ..
means any number of arguments of any type. 请注意,
..
表示任何类型的任意数量的参数。
Unfortunately, @args
supports at most one .. wildcard , so as long as You decide on having those annotated arguments either at the start or at the end of your argument list, You should be fine. 不幸的是,
@args
支持一个..通配符 ,因此只要您决定在参数列表的开始或末尾使用这些带注释的参数,就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.