AspectJ の declare precedence の挙動を確認してみた。
以下のようなクラス App とアスペクト AspectX があるとする。
public class App {
void foo() {
System.out.println("hello");
}
}aspect AspectX {
pointcut x(): execution(* *.foo(..));
before() : x() {
System.out.println("before x");
}
Object around(): x() {
System.out.println("around(1) x");
Object result = proceed();
System.out.println("around(2) x");
return result;
}
after() : x() {
System.out.println("after x");
}
}これを実行すると、以下のような出力になる。
| before x |
| around(1) x |
| hello |
| around(2) x |
| after x |
ここで更に、AspectX とほぼ等価な内容の AspectY を追加したらどうなるか。
aspect AspectY {
pointcut y(): execution(* *.foo(..));
before() : y() {
System.out.println("before y");
}
Object around(): y() {
System.out.println("around(1) y");
Object result = proceed();
System.out.println("around(2) y");
return result;
}
after() : y() {
System.out.println("after y");
}
}出力は以下のようになる。分かりやすいように AspectX の出力を赤、AspectY の出力を青に色分けした。
| before x |
| around(1) x |
| before y |
| around(1) y |
| hello |
| around(2) y |
| after y |
| around(2) x |
| after x |
上の例では、たまたまAspectX が優先となっているが、この優先順位を明示するには、
declare precedenceを記述する。(ここでは AspectY に追加した。)
aspect AspectY {
declare precedence: AspectY, AspectX;
pointcut y(): execution(* x.foo(..));
before() : y() {
・・・略こうすると、以下のように前後関係が逆転する。
| before y |
| around(1) y |
| before x |
| around(1) x |
| hello |
| around(2) x |
| after x |
| around(2) y |
| after y |
0 件のコメント:
コメントを投稿