レベルエンター山本大のブログ

面白いプログラミング教育を若い人たちに

BLOCKVROCKリファレンス目次はこちら

[翻訳]Guice User's Guide ■27. Intercepting Methods

メソッドのインターセプト

Guice supports simple method interception using the AOP Alliance API. You can bind interceptors from your modules using Binder. For example, to apply a transaction interceptor to methods annotated with @Transactional:

GuiceAOPアライアンスのAPIを使った簡単なメソッドインターセプトをサポートします。モジュールからBinderを使ってインターセプターをバインドすることができます。たとえば、トランザクションインターセプターをメソッドに適用するには、@Transactionalアノテーションを使って注釈します。

import static com.google.inject.matcher.Matchers.*;

...

binder.bindInterceptor(
  any(),                              // Match classes.
  annotatedWith(Transactional.class), // Match methods. 
  new TransactionInterceptor()        // The interceptor.
);

Try to shoulder as much of the filtering as is possible on the matchers rather than in the interceptor's body as the matching code runs only once at startup.

起動時に一度だけインターセプターの内容と一致したコードが実行されるようにするよりも、matchersでできるだけ多くのフィルタリングを負担するようにしてください。