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常用アノテーション
- トランザクション
- @Transaction
- メソッド正常終了時コミット
- メソッド異常終了時ロールバック
- 認証
- キャシュー
- 非同期処理
- リトライ処理