gpt4 book ai didi

java - AspectJ MethodSignature 为 getParameterNames() 返回 null

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:33:35 25 4
gpt4 key购买 nike

我有一个方面根据目标方法的细节进行各种计算,因此预先提取这些内容如下:

    @Around("execution(* com.xyz.service.AccountService.*(..))")
public void validateParams(ProceedingJoinPoint joinPoint) throws Throwable {
final MethodSignature signature = (MethodSignature) joinPoint.getSignature();

final String methodName = signature.getName();
final String[] parameterNames = signature.getParameterNames();
final Object[] arguments = joinPoint.getArgs();
...
...
...
joinPoint.proceed();
}

在提取的详细信息中,除了始终返回 null 的 parameterNames 之外,所有信息都反射(reflect)了预期的信息。我希望它按照下面的签名返回 {accountDetails}。谁知道我可能遗漏了什么,或者这是一个错误?

这是我正在处理的目标方法的签名:

Long createAccount(RequestAccountDetails accountDetails);

最佳答案

对我有用:

@Aspect
public class MyAspect {

@Around("execution(* *(..)) && !within(MyAspect)")
public Object validateParams(ProceedingJoinPoint joinPoint) throws Throwable {
final MethodSignature signature = (MethodSignature) joinPoint.getSignature();

final String[] parameterNames = signature.getParameterNames();
for (String string : parameterNames) {
System.out.println("paramName: " + string);
}

return joinPoint.proceed();

}
}

输出:paramName: accountDetails

我已将 validateParams 的签名更改为:public Object validateParams(ProceedingJoinPoint joinPoint) throws Throwable 因为 createAccount() 返回一个 Long。否则我会得到错误:applying to join point that doesn't return void: {0}

关于java - AspectJ MethodSignature 为 getParameterNames() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8859987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com