问题描述
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();
我将parameterNames设置为null.如何获取parameterNames?
I am getting parameterNames as null.How do I get the parameterNames?
推荐答案
我只是在普通的AspectJ中签入,而在AspectJ 1.8.6中,我从未获得过null
作为参数名.也许您使用的是旧版本,并且需要升级到当前版本(1.8.9).如果所讨论的类文件是使用相应的调试信息编译的,则参数名称将正确显示.但是,即使剥离了调试信息,或者您正在访问JDK方法的参数名称,至少AspectJ也会吐出arg0
,arg1
等之类的名称.
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.
更新:仅在具有JDK动态代理的基于代理的Spring AOP中,该问题在纯AspectJ或Spring应用程序中使用的AspectJ LTW中不存在.我可以通过从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中将parameterNames获取为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!