Spring AOP :- Getting parameterNames as null in the joinPoint

巧了我就是萌 提交于 2019-12-23 13:25:14

问题


LoggingAspect.java

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

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

Object[] arguments = joinPoint.getArgs();

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


回答1:


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.

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.

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. :-)



来源:https://stackoverflow.com/questions/38539797/spring-aop-getting-parameternames-as-null-in-the-joinpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!