AspectJ中相同方法的@Around和@After Throwing不能正常工作。

huangapple 未分类评论43阅读模式
英文:

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 的任何方法,但是 HandleExceptionAdviceAuthorizeAdvice 抛出 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.

huangapple
  • 本文由 发表于 2020年7月26日 01:12:31
  • 转载请务必保留本文链接:https://java.coder-hub.com/63091200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定