英文:
AspectJ @Around and @After throwing on same method is not working
问题
我在一个方法上使用了两个注解:@Around 和 @After throwing。@Around 方法执行授权操作,@After throwing 处理授权抛出的异常。以下是示例:
class AuthClass {
    @Authorize
    @HandleException
    public String isValidUser() {
        return "yes";
    }
}
public aspect AuthorizeAdvice {
    declare precedence: HandleExceptionAdvice;
    pointcut authorizeRequest(): execution(@Authorize * *(..));
    Object around(): authorizeRequest() {
        // 调用可能抛出异常的方法
        throw new AppException();
    }
}
public aspect HandleExceptionAdvice {
    pointcut handleException(): execution(@HandleException * *(..));
    after() throwing(AppException e): handleException() && 
        !within(HandleException) {
        // 在这里处理 AppException
    }
}
public aspect CachingAspect {
    pointcut cachePut(): execution(@Cacheable * *(..));
    Object around(): cachePut() {
      // 执行缓存操作
    }
}
我的问题是,在 AuthorizeAdvice 切面抛出 AppException 时,HandleExceptionAdvice 切面会拦截 AppException 并正确处理。但是当我添加了另一个切面 CachingAspect 后,CachingAspect 并未应用于 AuthClass 的任何方法,但是 HandleExceptionAdvice 在 AuthorizeAdvice 抛出 AppException 时却没有拦截到 AppException。
英文:
I have two applied @Around and @After throwing annotation on a method. @Around method is doing Authorization, @After throwing is handling the exception when Authorization throws an exception, see the example below:
class AuthClass {
    @Authorize
    @HandleException
    public String isValidUser() {
        return "yes";
    }
}
public aspect AuthorizeAdvice {
    declare precedence : HandleExceptionAdvice;
    pointcut authorizeRequest() : execution (@Authorize * *(..));
    Object around() : authorizeRequest() {
        //call method which throws exception
        throw new AppException();
    }
}
public aspect HandleExceptionAdvice {
    pointcut handleException() : execution (@HandleException  * *(..));
    after() throwing(AppException e) : handleException() && 
        !within(HandleException) {
        //handle AppException here
    }
}
public aspect CachingAspect {
    pointcut cachePut() : execution(@Cacheable * *(..));
    Object around() : cachePut() {
      //do caching stuff
    }
}
My question here is, when AuthorizeAdvice aspect throws AppException, HandleExceptionAdvice aspect intercepts the AppException and handling the exception perfectly. But the problem started when I added one more aspect CachingAspect. CachingAspect is not applied to any methods of AuthClass but HandleExceptionAdvice is not intercepting AppException when AuthorizeAdvice throws AppException.
专注分享java语言的经验与见解,让所有开发者获益!



评论