Seasar.NETのQuillで独自インターセプターを書いてAOPしてみる

via. http://banban55.dip.jp/~ragnarok/blog/archives/cat3/index.html


AbstractInterceptorを継承して自前の差し込み処理クラスを作る。

Imports Seasar.Framework.Aop.Interceptors

Public Class MyInterceptor
    Inherits AbstractInterceptor

    Public Overrides Function Invoke(ByVal aInvocation As Seasar.Framework.Aop.IMethodInvocation) As Object
        Try
            '前処理をここに書く
            MsgBox("Before")

            'メソッドを呼び出す
            Dim result As Object = aInvocation.Proceed

            '後処理をここに書く
            MsgBox("After")
            Return result
        Catch ex As Exception
            Throw
        Finally
        End Try
    End Function

End Class


サービスクラスに差し込みクラスをアスペクトする。

Imports Seasar.Quill.Attrs

Public Class AbcServiceImpl
    Implements IAbcServices

    Protected dao As IAbcDao

    <Aspect(GetType(MyInterceptor))>
    Public Overridable Function GetAll(ByVal aEntity As AbcEntity) As System.Collections.IDictionary Implements IAbcServices.Execute
        Return dao.SelectAll()
    End Function

End Class


これでGetAll()を実行すると処理の前後にメッセージが表示される。とっても簡単。使いどころは色々ありそう。