Page List

Search on the blog

2012年8月29日水曜日

Spring Framework -Aspect Oriented Programming-

Spring特集第3弾。Aspect Oriented Programming(以下AOP)について書きます。

AOPとは?
共通的な機能を後から付け加える仕組みです。 具体的に言うと、
  1. ロギング
  2. キャッシング
  3. セキュリティ
  4. 認証
などの共通的に利用される機能をビジネスロジックから切り離して、後から横断的に追加できるような仕組みのことです。

Before AdviceとAfter Advice
まずは、一番簡単なBefore AdviceとAfter Adviceの例を書きます。Adviceとは、AOPの用語で追加する共通処理のことです。何をやっているかは以下のサンプルを見れば一目瞭然です。
  1. // src/cc/co/goodpreparations/Person.java  
  2. package cc.co.goodpreparations;  
  3.   
  4. public class Person {  
  5.     public void smile() {  
  6.         System.out.println("s/he is smiling.");  
  7.     }  
  8.       
  9.     public void cry() {  
  10.         System.out.println("s/he is crying.");          
  11.     }  
  12. }  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!-- // src/Beans.xml -->  
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  7.     http://www.springframework.org/schema/aop   
  8.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">  
  9.   
  10.     <aop:config>  
  11.         <aop:aspect id="myaop" ref="aoptest">  
  12.             <aop:pointcut id="all"  
  13.                 expression="execution(* cc.co.goodpreparations.*.*(..))" />              
  14.             <aop:pointcut id="smile"  
  15.                 expression="execution(* cc.co.goodpreparations.*.smile(..))" />  
  16.             <aop:before pointcut-ref="all" method="beforeAdvice" />  
  17.             <aop:after pointcut-ref="smile" method="afterAdvice" />  
  18.         </aop:aspect>  
  19.     </aop:config>  
  20.   
  21.     <bean id="person" class="cc.co.goodpreparations.Person">  
  22.     </bean>  
  23.   
  24.     <bean id="aoptest" class="cc.co.goodpreparations.AOPTest" />  
  25. </beans>  
  1. // src/cc/co/goodpreparations/AOPTest.java  
  2. package cc.co.goodpreparations;  
  3.   
  4. import org.aspectj.lang.ProceedingJoinPoint;  
  5.   
  6. public class AOPTest {  
  7.   
  8.     public void beforeAdvice() {  
  9.         System.out.println("@This is a before advice.");  
  10.     }  
  11.   
  12.     public void afterAdvice() {  
  13.         System.out.println("@This is an after advice.");  
  14.     }  
  15. }  
  1. // src/cc/co/goodpreparations/Main.java  
  2. package cc.co.goodpreparations;  
  3.   
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. public class MainApp {  
  8.    public static void main(String[] args) {  
  9.       ApplicationContext context =   
  10.              new ClassPathXmlApplicationContext("Beans.xml");  
  11.   
  12.       Person person = (Person) context.getBean("person");  
  13.         
  14.       person.smile();  
  15.       System.out.println("---------------------------------");  
  16.       person.cry();  
  17.    }  
  18. }  
ポイントとなる設定ファイルの書き方を説明します。AOPの設定は、aop:config要素の中に書きます。aop:aspectの中に、Aspectの設定を書きます。Aspect=Advice + (処理を埋め込む場所)です。ちなみに(処理を埋め込む場所)のことをPointcutと呼びます。
Aspect = Advice + Pointcut
と覚えておくとよいです。設定ファイルの説明に戻ります。簡単に設定箇所を纏めると以下のようになります。
  1. aop:aspect要素のid属性にAspectのIDを、ref属性にAdviceが定義されたクラスのBean IDを書きます。
  2. aop:pointcut要素のid属性にPointcutのIDを、expression属性に共通処理を埋め込みたい場所を正規表現で書きます。
  3. aop:before要素のpointcut-ref属性に使用するPointcutのIDを、method属性に1.で指定したBeanのメソッド名を書きます。(このメソッドがAdviceになる)
  4. aop:after要素もaop:before要素と同様に書きます。
実行結果は、以下のようになります。
@This is a before advice.
s/he is smiling.
@This is an after advice.
---------------------------------
@This is a before advice.
s/he is crying.
このようにAOPを利用すると、任意の処理を任意の場所に後から埋め込むことができます。

その他のAdvice
Before Advice、After Adviceの他にもAdviceはあります。以下にAdviceの種類を纏めます。
種類 用途
Before Advice 処理をメソッドの実行前に追加したい場合。
After Advice 処理をメソッドの実行後に追加したい場合。
Around Advice 処理をメソッドの実行前後に追加したい場合。
After-returning Advice 処理をメソッドが正常終了した後に追加したい場合。
After-throwing Advice 処理をメソッドが異常終了した(例外がスローされた)後に追加したい場合。
このように、どのタイミングでAdviceを挿入したいかに応じて様々な種類のAdviceを使うことができます。それぞれのAdviceの使用方法はSpringSource.orgのreferenceを参照してください。

まとめ
DIがオブジェクト間の依存性を外部に掃き出し、それを後から注入する機能だと考えると、AOPはオブジェクトと共通機能の依存性を外部に掃き出し、それを後から注入する機能だと考えることができます。AOPを使うことで機能追加/変更に強いソースコードを書くことができます。

0 件のコメント:

コメントを投稿