Seasar2 S2AOPについて
ページ 目次
- ThrowsInterceptorについて
1.ThrowsInterceptorについて
前回のS2AOPではログの出力ということでTraceInterceptorの場合を見てみました。
今回は同様にあらかじめ用意されているインターセプターであるThrowsInterceptorを使用してみたいと思います。
ThrowsInterceptorは例外が発生した場合の処理を記述することができます。
では実際に作成してみてみたいと思います。
作成するファイルは以下のようになります。
パッケージ名 | クラス名 | 説明 |
---|---|---|
sample.aop | AopThrowsMain.java | サンプルのメインクラス |
sample.aop | AopThrowsSample.java | 対象のコンポーネントにAOPとしてかける例外処理クラス |
sample.aop | AopThrowsService.java | サンプルクラスから使用されるインターフェース |
sample.aop | AopThrowsServiceImpl.java | サンプルクラスから使用される実装クラス |
sample.aop | aopThrowslnterceptor.dicon | 設定ファイル |
AopThrowsMain.java
package sample.aop; import org.seasar.framework.container.S2Container; import org.seasar.framework.container.factory.SingletonS2ContainerFactory; /** * AopをかけたServiceクラスを呼び出すMainクラス。 * */ public class AopThrowsMain { // 設定ファイルのPath private static final String PATH = "sample/aop/aopThrowslnterceptor.dicon"; /** * メインクラス。 * @param args */ public static void main(String[] args) { // 設定ファイルを読み込む. SingletonS2ContainerFactory.setConfigPath(PATH); // 初期化する. SingletonS2ContainerFactory.init(); // コンテナを取得する. S2Container container = SingletonS2ContainerFactory.getContainer(); AopThrowsService alss = (AopThrowsService) container .getComponent(AopThrowsService.class); System.out.println("RuntimeExceptionが発生する場合"); try { alss.aopThrowsCheck(true); } catch (Exception e) { System.out.println("例外発生"); } System.out.println("Exceptionが発生する場合"); try { alss.aopThrowsCheck(false); } catch (Exception e) { System.out.println("例外発生"); } // 使用したコンポーネントを廃棄する. container.destroy(); } }
AopThrowsSample.java
package sample.aop; import org.aopalliance.intercept.MethodInvocation; import org.seasar.framework.aop.interceptors.ThrowsInterceptor; /** * 対象のコンポーネントにAOPとしてかける例外処理クラス. */ public class AopThrowsSample extends ThrowsInterceptor { /***/ private static final long serialVersionUID = 1L; /** * 対象のコンポーネントにAOPとしてかける例外処理メソッド. */ public void handleThrowable(Throwable t, MethodInvocation invocation) throws Throwable { if (t instanceof RuntimeException) { System.out.println("例外特定の例外のときに走らせる処理"); } else { System.out.println("それ以外の例外のとき"); throw t; } } }
AopThrowsService.java
package sample.aop; /** * Aopの対象のコンポーネント */ public interface AopThrowsService { /** * サンプルチェック. * @param arg 引数 * @return 戻り値 * @throws Exception 例外 * */ public boolean aopThrowsCheck(boolean arg) throws Exception; }
AopThrowsServiceImpl.java
package sample.aop; /** * Aopの対象のコンポーネント */ public class AopThrowsServiceImpl implements AopThrowsService { /** * サンプルチェック. * * @param arg 引数 * @return 戻り値 * @throws Exception 例外 */ public boolean aopThrowsCheck(boolean arg) throws Exception { System.out.println("サンプルメソッドの開始"); if (arg) { throw new RuntimeException(); } else { throw new Exception(); } } }
aopThrowslnterceptor.dicon
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN" "http://www.seasar.org/dtd/components24.dtd"> <components> <!-- ログ出力用のコンポーネント --> <component name="traceInterceptor" class="org.seasar.framework.aop.interceptors.TraceInterceptor"/> <!-- Throwslnterceptor用のコンポーネント --> <component name="throwsInter" class="sample.aop.AopThrowsSample"/> <!-- AOPログのコンポーネントの登録 --> <component name="AopThrowsService" class="sample.aop.AopThrowsServiceImpl"> <aspect>traceInterceptor</aspect> <aspect>throwsInter</aspect> </component> </components>
コンポーネントの実装クラスのメソッドaopThrowsCheckでは引数によって発生させる例外を分けています。
またhandleThrowableでは引数として渡された例外の種類によって動作を分けています。
設定ファイルであるaopThrowslnterceptor.diconでは、前回
行ったtraceInterceptorをかけている他に以下の部分が追加されています。
<component name="throwsInter" class="sample.aop.AopThrowsSample"/>
またコンポーネントの登録のところにて、以下の部分が追加されています。
<aspect>throwsInter</aspect>
では実際に実行してみたいと思います。
DEBUG S2Containerを作成します。path=sample/aop/aopThrowslnterceptor.dicon DEBUG S2Containerを作成しました。path=sample/aop/aopThrowslnterceptor.dicon INFO Running on [ENV]product, [DEPLOY MODE]Normal Mode RuntimeExceptionが発生する場合 DEBUG BEGIN sample.aop.AopThrowsServiceImpl#aopThrowsCheck(true) サンプルメソッドの開始 例外特定の例外のときに走らせる処理 DEBUG END sample.aop.AopThrowsServiceImpl#aopThrowsCheck(true) : null Exceptionが発生する場合 DEBUG BEGIN sample.aop.AopThrowsServiceImpl#aopThrowsCheck(false) サンプルメソッドの開始 それ以外の例外のとき DEBUG END sample.aop.AopThrowsServiceImpl#aopThrowsCheck(false) Throwable:java.lang.Exception 例外発生
実行結果ですが、まず対象のコンポーネントにtrueを渡した場合RuntimeExceptionが発生します。
するとAopThrowsSampleのメソッドであるhandleThrowableが呼ばれ、その内部で例外の種類が
RuntimeExceptionの場合の処理が行われています。
そしてその後呼出元であるAopThrowsMainのmainメソッドに戻っています。
このとき呼出元であるmainメソッドには例外が知らされていません。
次に、対象のコンポーネントにfalseを渡した場には、通常のExceptionが発生します。
その場合AopThrowsSampleのメソッドであるhandleThrowableの内部ではRuntimeException以外の場合の処理が行われています。
そしてその後再度 例外がスローされているのでAopThrowsMainのmainメソッドにてExceptionをキャッチしています。
このように対象のコンポーネントから例外が発生した場合に、特定の例外の場合には処理を行いたいという場合に、有効な手段となります。
Seasar2 Topに戻る