Using Ajc compiler with Spring problem AspectJ

寵の児 提交于 2019-12-02 03:08:40

It's difficult without knowing which line 76 is:

at com.cdf.dfdxc.aspect.logging.LoggingAspect.logEntry(LoggingAspect.java:76)

but you are using a very aggressive pointcut

@Pointcut("within(com.csc.exceed.uow.*)")
public void loggingAspect() {}

This matches all kinds of events, not only method executions, but also static and instance inititializers, field access etc. (see the Primitive Pointcuts overview in the AspectJ Quick Reference).

If any of these:

  • returns null for joinPoint.getTarget(), this line will throw a NPE:

    Class<? extends Object> clazz = joinPoint.getTarget().getClass();
    
  • returns null for joinPoint.getSignature(), this line will throw a NPE:

    String name = joinPoint.getSignature().getName();
    
  • Also, here you are checking for null or empty args:

    if (ArrayUtils.isEmpty(joinPoint.getArgs())) {
        if (!(name.startsWith("get")) || (name.startsWith("set")))
            logger.log(LogLevel.INFO, clazz, null, BEFORE_STRING, name,
    

    but you are using the args nevertheless:

                constructArgumentsString(clazz, joinPoint.getArgs()));
    

    This might also throw a NPE, depending on the code in constructArgumentsString()

Check which of these happens on line 76, and you have your candidate for failure. But my first hint would be to replace your aggressive catch-all pointcut with an execution or call pointcut.

I would use a combined pointcut, defined in a modular way:

// you can easily reuse this
@Pointcut("within(com.csc.exceed.uow.*)")
public void myApp() {}

// and this
@Pointcut("execution(* *.*(..))")
public void methodExecution(){}

// but this is the pointcut you are actually matching
@Pointcut("myApp() && methodExecution()")
public void methodExecutionInMyApp(){}

@Before("methodExecutionInMyApp()")
public void logMethodExecutions(JoinPoint jp){
    // your code here
}

Here's how to make the class thingy NPE-safe:

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