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等名称。

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

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

因此,如果您的配置如下所示...

<aop:aspectj-autoproxy/>

...只需将其更改为:

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

然后重试并享受。 :-)

09-10 08:36
查看更多