gpt4 book ai didi

java - Spring AOP :- Getting parameterNames as null in the joinPoint

转载 作者:搜寻专家 更新时间:2023-11-01 03:33:50 27 4
gpt4 key购买 nike

LoggingAspect.java

@Around("allGenericAppServiceImplMethods()")
public Object LoggingAdvice(ProceedingJoinPoint joinPoint)throws Throwable{

MethodSignature signature = (MethodSignature)joinPoint.getSignature();
String[] parameterNames = signature.getParameterNames();

Object[] arguments = joinPoint.getArgs();

我得到的 parameterNames 为 null。我如何获得 parameterNames?

最佳答案

我刚刚检查了普通的 AspectJ,但在 AspectJ 1.8.6 中,我从未将参数名称设置为 null。也许您使用的是旧版本,需要升级到当前版本 (1.8.9)。如果有问题的类文件是使用相应的调试信息编译的,则参数名称会正确显示。但是,即使删除了调试信息或者您正在访问 JDK 方法的参数名称,至少 AspectJ 会吐出像 arg0arg1 等这样的名称。

更新:该问题在纯AspectJ 或Spring 应用程序中使用的AspectJ LTW 中不存在,仅在使用JDK 动态代理的基于代理的Spring AOP 中存在。我可以使用从 GitHub 某处克隆的一个小示例项目重现该问题。

如果您针对接口(interface)进行编程,解决方案是对代理强制使用 CGLIB,即使这里的默认设置是 JDK 代理也是如此。

所以如果你的配置看起来像这样......

<aop:aspectj-autoproxy/>

...只需将其更改为:

<aop:aspectj-autoproxy/>
<aop:config proxy-target-class="true">
<!-- other beans defined here... -->
</aop:config>

然后重试并享受。 :-)

关于java - Spring AOP :- Getting parameterNames as null in the joinPoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38539797/

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