2016-07-23 42 views
2

LoggingAspect.java的Spring AOP: - 获取的连接点

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

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

Object[] arguments = joinPoint.getArgs(); 

我越来越parameterNames parameterNames为null作为null.How我会得到parameterNames?

回答

1

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

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

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

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

<aop:aspectj-autoproxy/> 

...只是将其更改为:

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

,然后再次尝试和享受。 :-)

+0

你好@ kriegaex!我目前正在使用AspectJ 1.6.11。我会升级到最新版本,并检查.... –

+0

我刚刚将我的aspectj的maven依赖项升级到1.8.9 ....仍然我得到parameterNames为null ...这个问题是与jdk或cglib代理相关的东西吗? ... –

+0

我已经更新了我的答案。 – kriegaex