LoginSignup
0
1

More than 5 years have passed since last update.

はじめてのSpring AOP

Last updated at Posted at 2018-01-23

はじめに

はじめてSpring AOPを触ったので導入手順をまとめた。

実装手順

1. 依存を追加

mavenの場合

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

gradleの場合

compile('org.springframework.boot:spring-boot-starter-aop')

2. Adviceするクラスを作成

差し込みたい処理(adviceMethod)に対して、アノテーションを付与する。
アノテーションがAdviceの種類で、アノテーションのvalueが差し込む対象となる。

@Aspect
@Component
public class SampleAdvice {

    @Before("execution(* com.example.app.controller.*.*(..))")
    public void adviceMethod() {
        System.out.println("adviceMethod : before");
    }
}

この場合はcom.example.app.controllerパッケージに属するクラスの関数に対して、adviceMethod()の処理を差し込む。
アノテーションは@Beforeなので、対処の関数の実行前にadviceMethod()が実行される。

3. 完成

めっちゃ簡単でした\(^o^)/

参考資料

Adviceアノテーションの種類や、valueの指定は下記の記事を参考にしました。
https://qiita.com/NagaokaKenichi/items/386af61b6866d60964e8

0
1
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
1