LoginSignup
1

More than 5 years have passed since last update.

spring framework 簡易勉強メモ(二):AOP

Posted at

spring framework 簡易勉強メモ(二):AOP

本質

  • 散在コードを本質処理から分離

考え方

  • 散在処理を横断的に実装

代表的な応用場面

  • ログ出力
  • セキュリティチェック
  • トランザクション
  • キャシュー
  • モニタリング
  • 例外ハンドリング

ソース例

@Aspect
@Component
public class MethodStartLoggingAspect {
    @Before("execution(* *..*ServiceImpl.*(..))")
    public void startLog(JoinPoint jp) {
        System.out.println("メソッド開始:" + jp.getSignature());
    }
}

Advice実装方法

  • @Before
    • join point 前実施
  • @AfterReturning
    • join point が正常終了後実施
    • 例外スロー時実施しない
  • @AfterThrowing
    • 例外スロー時実施
    • join point が正常終了後実施しない
  • @After
    • 例外スローにかかわらず終了後実施
  • @Around
    • join point の前後で実施

point cut 式

  • join pointを選択する方法
  • 例:"execution(* ..*ServiceImpl.(..))"
  • 指定の方法
    • メソッド指定
    • クラス型指定
    • 名前などその他指定

AOP常用アノテーション

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
1