SimpleController4Jでは、リクエストパス・HTTPメソッドに応じて、フロントコントローラ(DispatcherServlet)
が実行するメソッドを決定して、処理のディスパッチを実行します。
ディスパッチ先の実行メソッドの以下のポイント(タイミング)をジョンインポイント(JoinPoint)として、
処理を挿入することが出来ます。
SimpleController4Jで、挿入処理を記述したアスペクト(Aspect)と認識されるためには、
@Aspectアノテーションをクラスに記述します。
例としては以下のようになります。
/** * アスペクトサンプル */ @Aspect public class SampleAspect { 〜 メソッド定義など 〜 }
各ジョインポイントアノテーション(@Before/@After/@Around/@AfterReturning/@AfterThrowing)には、
execution属性を指定することが出来ます。
このexecution属性に指定したメソッドが実行される場合に、挿入処理が実行されます。
execution属性は以下の指定が可能です。
まず、基本的な指定方法は「クラス完全修飾名 + メソッド名」となります。
以下は、コントローラクラスとメソッドを完全修飾名で指定する例です。
「controller.SampleController」クラスの「getMethod」メソッドが実行される前に、
アドバイスが実行されます。
/** * アスペクトサンプル */ @Aspect public class SampleAspect { @Before(execution = "controller.SampleController.getMethod") public void beforeGet(ServletResponse response) throws IOException { PrintWriter out = ((HttpServletResponse) response).getWriter(); out.println("before aspect execute!"); } }
■ 実行結果 CW-02:~ XXX$ curl localhost:8080/SimpleControllerSample/sample/get before aspect execute! getMethod get execute!
次に「ワイルドカード指定」の方法ですが、「ワイルドカード:*」を指定した箇所は任意となり、
その他の指定部分にマッチするクラス・メソッドが実行される時に、アドバイスが実行されます。
以下は、複数のコントローラクラスの「init」メソッドが実行される後に、アドバイスが実行されます。
/** * アスペクトサンプル */ @Aspect public class SampleAspect { @After(execution = "*.init") public void afterInit(ServletResponse response) throws IOException { PrintWriter out = ((HttpServletResponse) response).getWriter(); out.println("after aspect execute!"); } }
■ 実行結果 CW-02:~ XXX$ curl localhost:8080/SimpleControllerSample/sample/init init execute! after aspect execute!
同じアドバイスを複数の実行メソッド(ディスパッチメソッド)で実行したい場合があると思います。
そのような場合は、execution属性を複数設定することが出来ます。
以下は、複数のコントローラクラスの「postMethod」メソッド・「aroundMethod」メソッドが
実行される前後に、アドバイスが実行されます。
/** * アスペクトサンプル */ @Aspect public class SampleAspect { @Around(execution = {"*.postMethod", "*.aroundMethod"}) public void around(ServletRequest request, ServletResponse response) throws IOException { PrintWriter out = ((HttpServletResponse) response).getWriter(); out.println("around aspect execute!"); } }
■ 実行結果 CW-02:~ XXX$ curl -X POST localhost:8080/SimpleControllerSample/sample/post around aspect execute! postMethod post execute! around aspect execute! CW-02:~ XXX$ curl localhost:8080/SimpleControllerSample/sample/around around aspect execute! aroundMethod get execute! around aspect execute!
アドバイスメソッドには以下のパラメータを指定可能です。
SimpleController4Jへのご意見やご感想がございましたら、メッセージをお願い致します。
また、SimpleController4Jを使用して頂きまして、バグがございましたらご連絡を頂けると助かります。
皆様からのご意見・ご感想をもとに、SimpleController4Jを改善・拡張をしていきたいと考えております。
皆様からのメッセージは当方で確認をしておりますが、仕事をしながらの開発となる為、すぐにご返信・対応をすることが
出来ない場合がございます。何卒ご容赦ください。
タイトル※ | |
---|---|
メールアドレス | |
内容※ |