アスペクトの読み込み
S2AOP は S2Container とセットになった機能になっています。アスペクト定義は ComponentDef に含まれているはずなので、まずはどのように ComponentDef に登録されるのか見てみましょう。以前の記事を踏まえるとアスペクトの読み込みを行っているのは AspectTagHandler です。
public void start(TagHandlerContext context, Attributes attributes) { AspectDef aspectDef = null; String pointcutStr = attributes.getValue("pointcut"); if (pointcutStr != null) { String[] methodNames = StringUtil.split(pointcutStr, ", "); aspectDef = createAspectDef(createPointcut(methodNames)); } else { aspectDef = createAspectDef(); } context.push(aspectDef); }
ちゃんと createAspectDef や createPointcut が protected メソッドになっているのは好印象。拡張用意な設計です。
pointcut タグがあればポイントカットを作って AspectDef を生成しています。Pointcut はアスペクトをどこに適用するか、の定義。メソッド名やクラスを指定可能。正規表現も使えます。AspectDef はポイントカットに織り込まれる処理を記述した MethodInterceptor と、その対象となる Pointcut を保持する Bean。
end タグを処理する際、ComponentDef は AspectDef を登録・取得出来ることを示す AspectDefAware を実装しているので、ComponentDef に生成した Aspect を登録しています。
簡単ですね。では次は AspectDef をどのようにコンポーネントに織り込んでいるか見てみましょう。