Spring AOP :- 在 joinPoint 中获取参数名称为 null [英] Spring AOP :- Getting parameterNames as null in the joinPoint

查看:274
本文介绍了Spring AOP :- 在 joinPoint 中获取参数名称为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LoggingAspect.java

LoggingAspect.java

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

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

Object[] arguments = joinPoint.getArgs();

我得到的参数名称为 null.我如何得到参数名称?

I am getting parameterNames as null.How do I get the parameterNames?

推荐答案

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

I just checked in plain AspectJ and I never get parameter names as null with AspectJ 1.8.6. Maybe you use an older version and need to upgrade to the current version (1.8.9). The parameter names are displayed correctly if the class files in question were compiled with the corresponding debug info. But even if the debug info was stripped or you are accessing parameter names for JDK methods, at least AspectJ will spit out names like arg0, arg1 etc.

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

Update: The problem does not exist in pure AspectJ or in AspectJ LTW used in Spring applications, only in proxy-based Spring AOP with JDK dynamic proxies. I could reproduce the problem with a little sample project I cloned from somewhere on GitHub.

如果您针对接口进行编程,则解决方案是强制对代理使用 CGLIB,即使此处默认为 JDK 代理.

The solution if you are programming against interfaces is to enforce CGLIB usage for proxies even though the default here is JDK proxies.

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

So if your configuration looks like this ...

<aop:aspectj-autoproxy/>

...只需将其更改为:

... just change it to:

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

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

Then try again and enjoy. :-)

这篇关于Spring AOP :- 在 joinPoint 中获取参数名称为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆