【问题标题】:precedence order for annotations in aspectjaspectj 中注释的优先顺序
【发布时间】:2013-05-27 18:55:29
【问题描述】:

我有两个使用 AspectJ 编译时间编织的不同注释,比如说

1) 用于重试——如果抛出特定异常,该方法将自行重试
2) 翻译异常——即如果抛出特定异常,则会将该源异常转换为指定的目标异常

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

我怎样才能实现这种类型的功能 `

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
    }

`

有没有办法保证注解的操作顺序。

现在当我运行代码时,重试注释首先运行,然后异常翻译注释起作用。

【问题讨论】:

    标签: java exception-handling annotations aspectj


    【解决方案1】:

    使用declare precedence 来定义您想要的方面优先顺序。有关此优先级和默认优先级的更多信息,请参见 AspectJ 编程指南,Language Semantics 部分。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      相关资源
      最近更新 更多