LoginSignup
0
0

More than 3 years have passed since last update.

Java tips - Spring executionまとめ

Posted at

Spring executionまとめ

Springでメソッドを指定してAOPするときに、executionを用いるやり方があります。
その時に、executionってどうやって書くんだっけとなりやすいので備忘録としてまとめます。

executionの書き方

例えば以下のようなテストメソッドがあるとします。

package test;

public class TestC {
    public String testM(String str) throws Exception {
        return "test";
    }
}

このテストメソッドに前処理のAOPを用いる時は以下のように書きます。

@Before("execution(public String test.TestC.testM(String)) throws Exception")

要素としては以下の順番に書きます。
- 修飾子
- 戻り値の型
- パッケージ名
- クラス名
- メソッド名
- 引数の型
- 例外

この中で修飾子と例外は省略が可能です。そのため、以下のように書くことが可能。

@Before("execution(String test.TestC.testM(String))")

さらに、戻り値やパッケージ名やクラス名はワイルドカードを使用することができます。

@Before("execution(* test.*.*(..))")

まとめ

いつも忘れるので、まとめて思い出せるようにするだけで価値があるかな。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0