Springのわかりやすい資料

Springでbean定義の上書き - oknknicの日記」から更に更に引き続き、Springネタ。
Springのすごくわかりやすい資料があったのでメモしておく。

以下の内容が書かれていて、それぞれコード例を交えて説明されている。

以下、その一部を抜粋ベースでメモしておく。

アノテーション

種類
  • @Autowired, @Qualifier
  • @Resource
  • @Required
  • @Component
  • @Repository
  • @Service
  • @Controller
  • @Scope
  • @PostConstruct, @PreDestroy
  • AOP関連 ※org.aspectj:aspectjrtのjar、cglibのjarが必要
    • @Aspect
    • @Pointcut
    • @Order
    • @Before
    • @AfterReturning
    • @AfterThrowingAdvice
    • @AfterAdvice
    • @AroundAdvice
アノテーションの有効化設定
  • @Autowired, @Resource などの利用
<context:annotation-config>
  • @Component などの利用
<context:component-scan base-package="jp.co.hoge">

※こちらを設定した場合は annotation-config は指定不要
※include-filter, exclude-filterにより、検出対象の制御が可能

<aop:aspectj-autoproxy/>

AOP

ポイントカットの書式
  • execution: メソッド実行
  • within: 型やパッケージを制限
  • this: 指定インタフェースを実装するProxyに制限
  • target: 指定インタフェースを実装するターゲットに制限
  • args: 引数の数と型で制限
  • @within: 指定のアノテーションがクラス宣言に含まれているターゲットに制限
  • @target: 指定のアノテーションを持つターゲットに制限
  • @args: 指定のアノテーションを引数が含むものに制限
  • @annotation: 指定のアノテーションを持つメソッドに制限
  • bean: Spring管理下のBeanに制限
@Aspect
@Repository
public class HogeAspect {
  @Pointcut("execution(public * jp.co.hoge.HogeImpl.*(..))")
  private void hogeOpe(){};

  @Before("hogeOpe()") //pointcutを指定
  public void beforeAdvice() {
  }

  @AfterReturning("within(jp.co.hoge.*)") //join pointを直接指定
  public void afterReturningAdvice() {
  }
}