问题
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