javaspringspring-bootaspectjaspect

Exception handling with aspect oriented programming


I am new to Aspect-oriented programming. I am working on a spring-boot application with AspectJ, that has already an exception handling aspect as follows:

@Aspect
@Order(0)
public class ExceptionAspect {
 

  public ExceptionAspect() {
  }

  @Pointcut("within(com.mycom.service.impl..*)")
  public void applicationServicePointcut() {
  }

  @AfterThrowing(
    pointcut = "applicationServicePointcut()",
    throwing = "e"
  )
  public void translate(JoinPoint joinPoint, Throwable e) {
    ...
     //EXCEPTION HANDLING LOGIC
  }
}

Now I would like to add one more aspect, say NewAspect for the same pointcut as mentioned above i.e.

  "within(com.mycom.service.impl..*)"

Also I want any exception happening in the NewAspect to be handled by the ExceptionAspect.

In this regard, I am not able to understand, if the Order of the new aspect should be more than the existing ExceptionAspect or not.


Solution

  • The @Order annotation or implementing the @Ordered interface are Spring-specific and therefore only have an effect on Spring-managed components. AspectJ is a product independent of Spring. It has its own way of declaring and handling precedence:

    P.S.: I asked whether you use native AspectJ or Spring AOP because of the ordering annotations and also because in Spring AOP one aspect cannot advise another one. So what you want is only possible in native AspectJ in the first place.