aspectj中注释的优先顺序

时间:2013-05-27 18:55:30

标签: java exception-handling annotations aspectj

我有两个不同的注释使用AspectJ Compile时间编织,例如

1)重试 - 如果抛出特定异常,该方法将重试自己 2)翻译异常 - 即如果抛出特定异常将把源异常转换为指定的目标异常

如何定义这两个注释的工作顺序。这两个注释都是使用aspectj实现的,并使用@around advice。

我如何实现此类功能 `

Case 1
    @retry(IllegalArgumentException.class)
    @translate(source = IllegalArgumentException , target = IllegalStateException)
    void method() {
        //if it throws IllegalArgument exception 
        //method should retry and translate it to IllegalState after it
    }

case2
    @translate(source = IllegalArgumentException , target = IllegalStateException)
    @retry(IllegalStateException.class)
    void method() {
        //methodthrows IllegalArgument exception which gets translated to IllegalState
        //method should retry for IllegalStateException after it
    }

`

可以通过这种方式确保注释的操作顺序。

现在,当我运行代码时,首先运行注释,然后运行异常转换注释。

1 个答案:

答案 0 :(得分:1)

使用declare precedence来定义所需的宽高优先顺序。有关此优先级和默认优先级的更多信息,请参阅“AspectJ编程指南”的Language Semantics部分。

如果建议订单应该是动态的,那么最好的办法是让一个切入点同时捕获注释并根据注释的值来决定订单,这些值可以通过反射来确定。