Spring AOP:- 在 joinPoint 中获取参数名称为 null

2024-04-12

LoggingAspect.java

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

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

Object[] arguments = joinPoint.getArgs();

我获取的参数名称为空。如何获取参数名称?


我刚刚签入普通 AspectJ,但从未获得参数名称null使用 AspectJ 1.8.6。也许您使用旧版本并需要升级到当前版本(1.8.9)。如果相关类文件是使用相应的调试信息进行编译的,则参数名称将正确显示。但即使调试信息被删除或者您正在访问 JDK 方法的参数名称,至少 AspectJ 会吐出类似的名称arg0, arg1 etc.

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

如果您针对接口进行编程,解决方案是强制使用 CGLIB 代理,即使这里默认是 JDK 代理。

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

<aop:aspectj-autoproxy/>

...只需将其更改为:

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

然后再试一次并享受。 :-)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring AOP:- 在 joinPoint 中获取参数名称为 null 的相关文章

随机推荐