 |

|
DIxAOPコンテナ「Seasar2とSpring」
|
第7回:SpringによるAOPの導入
著者:豆蔵 長谷川 裕一、竹端 進 2006/1/18
|
|
|
前のページ 1 2 3 4 次のページ
|
 |
EmployeeManagerImplへのAOP適用
|
では、従業員管理Webアプリケーションを元に実際にAOPを適用していきましょう。
先に記述したように、Springが提供するインタセプターを用いたトレースログの出力と独自実装の例外処理を追加します。
|
Adviceの作成と定義ファイルの修正
|
まずは例外処理を実装したAdvice、ExceptionAdviceです(リスト10)。例外処理なのでThrowsAdviceを実装して作成します。今回はExceptionの例外に対してログを記述する処理を行うため、メソッドは1つです。
リスト10:例外処理
|
public class ExceptionAdvice implements ThrowsAdvice {
private static final Log log = LogFactory.getLog(ExceptionAdvice.class);
public void afterThrowing(Method m, Object[] args, Object o, Exception e)
throws Throwable {
log.error("Exception class["
+ o.getClass().getName()
+ "]: method["
+ m.getName() + "]:" + e);
throw e;
}
}
|
次にトレース処理ですが、これはSpringがデバッグ用に提供しているorg.springframework.aop.interceptor.DebugInterceptorを利用するため実装の必要はありません。DebugInterceptorを利用することでメソッドの開始と終了がトレースログとして出力されます。
DebugInterceptorの出力例
|
2006-01-13 18:18:06,492 DEBUG org.springframework.aop.interceptor.SimpleTraceInterceptor - Exiting invocation: method 'findAll', arguments []; target is of class [jp.co.thinkit.employee.business.EmployeeManagerImpl]; count=6
2006-01-13 18:18:37,821 DEBUG org.springframework.aop.interceptor.SimpleTraceInterceptor - Entering invocation: method 'addEmployee', arguments [jp.co.thinkit.employee.business.Employee@8a6351]; target is of class [jp.co.thinkit.employee.business.EmployeeManagerImpl]; count=7
|
実際にはデバッグ文は改行されず1行で出力されます(上記の例だと2行)。
以上でAdviceの実装は終わりです。以外にあっけなくできあがりましたね。
次にBean定義ファイルを修正します。トレースログの出力処理と例外処理を利用できるように修正した部分を解説していきましょう。
リスト11はBean定義ファイルにEmployeeManager用のAOPを設定した部分です。
リスト11:EmployeeManager用のAOP設定

(画像をクリックすると別ウィンドウに拡大表示します)
リスト11の(3)は、例外用に作成したExceptionAdviceとSpringが提供するDebugInterceptorのBean定義です。
リスト11の(2)は、ExceptionAdviceとPointcutを1つにまとめたAdvisorの定義です。EmployeeManagerのすべてのメソッドを例外処理の対象とするためPointcutの条件に「.*.(すべてのメソッド)」を設定しています。
なお、DebugInterceptorにはAdvisorが必要ないことに注意してください。Springが提供するInetrceptorクラスはAdvisorなし(つまりPointcutなし)で使用することが可能です。その場合はターゲットとなるすべてのメソッドにInetrceptorクラスが適用されます。
リスト11の(1)で、ProxyFactoryBean(ProxyFactoryBeanについては本連載の「第4回:Springの導入によるDIの実現」を参照してください)をAOPとして追加する処理をします。また、リスト11の(2)までで定義したAdvisorとDebugInterceptorをAOPのtargetとして、今まで宣言トランザクションの対象としていたBean定義ファイル内でemployeeManagerTagetというidが付加されていたEmployeeManagerImplを設定しています。
AOPの設定の最後は、トランザクションの対象としてリスト11の(1)の設定で付加したid"employeeManagerAOP"をトランザクションの対象とすることです(リスト12)。
リスト12:EmployeeManager用のトランザクション設定
|
<!-- Transaction -->
<bean id="employeeManager"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" /></property>
<property name="target">
<ref local="employeeManagerAop" /></property> 
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED, -java.lang.Exception, readOnly</prop>
<prop key="update*">PROPAGATION_REQUIRED, -java.lang.Exception</prop>
</props>
</property>
</bean>
|
ここまでで、EmployeeManagerImplに対するAOPを利用したトレースログの出力と例外処理の追加は終わりです。
|
前のページ 1 2 3 4 次のページ
|

|
|

|
著者プロフィール
株式会社豆蔵 長谷川 裕一
XMLの技術開発やCORBA、EJBを使用したシステム開発などを経て、現在はアジャイル開発プロセスの導入から工学的なソフトウエアプロセスの策定、オープンソースプロダクトに関するコンサルタント、アーキテクトとして常に第一線で活躍。共著として「プログラムの育てかた 現場で使えるリファクタリング(ソフトバンク)」、「Spring入門(技術評論社)」。
|

|
著者プロフィール
株式会社豆蔵 竹端 進
鉄鋼系SIerを経て現職に。現在はオープンソースプロダクトに関するコンサティング、開発支援、教育を行うチームに所属。日々、新たな技術をどのように生かしていくかを考える毎日。現在の注目対象はSeasar2とMaven。
|
|
|
|