Seasar2でアノテーションを使って自動AOPその3
http://d.hatena.ne.jp/koichik/20050903#1125752410
更に問題点を指摘して頂きました。最初に作ったときに、S2の内部構造がよく分からないまま作ったのでコンポーネント検索の部分と初期化の部分にあまり自信が無かったのですが、案の定どちらも間違ってましたね(汗)・・・ご指摘ありがとうございます。
今の自分の作り方だと、親コンテナと子コンテナまでは検索出来ても、その先の孫コンテナの検索が抜けてますね・・・というわけで、まずはエラーになる状態を作り出してみます。diconの設定ファイルを以下のように分割。
app.dicon
<?xml version="1.0" encoding="Windows-31J"?> <!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN" "http://www.seasar.org/dtd/components21.dtd"> <components> <include path="test/application.dicon"/> </components>
test/common.dicon
<?xml version="1.0" encoding="Shift_JIS"?> <!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container//EN" "http://www.seasar.org/dtd/components.dtd"> <components namespace="common"> <include path="j2ee.dicon"/> <!-- アノテーション対応トランザクションインターセプター --> <component name="annotationAllTransaction" class="test.AnnotationTransactionInterceptor"> <property name="interceptorMap"> #{@test.TransactionType@Required : j2ee.requiredTx, @test.TransactionType@RequiresNew : j2ee.requiresNewTx, @test.TransactionType@Mandatory : j2ee.mandatoryTx, @test.TransactionType@NotSupported : j2ee.notSupportedTx} </property> </component> <!-- 初期化 --> <component name="autoRegisterInit" class="test.AutoRegisterInit"> <initMethod name="init"/> </component> <!-- トランザクション自動AOP --> <component name="annotationAllTransactionRegister" class="test.AnnotationAutoRegister"> <property name="annotationClass">@test.Transaction@class</property> <property name="interceptorName">"common.annotationAllTransaction"</property> </component> </components>
test/application.dicon
<?xml version="1.0" encoding="Windows-31J"?> <!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN" "http://www.seasar.org/dtd/components21.dtd"> <components> <include path="test/application_child1.dicon"/> <include path="test/application_child2.dicon"/> </components>
test/application_child1.dicon
<?xml version="1.0" encoding="Windows-31J"?> <!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN" "http://www.seasar.org/dtd/components21.dtd"> <components namespace="aop"> <include path="test/common.dicon"/> <!-- Component --> <component class="test.Action1"/> <component class="test.Action2"/> </components>
test/application_child2.dicon
<?xml version="1.0" encoding="Windows-31J"?> <!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN" "http://www.seasar.org/dtd/components21.dtd"> <components namespace="aop"> <include path="test/common.dicon"/> <!-- Component --> <component class="test.Action3"/> <component class="test.Action4"/> </components>
この状態で、id:da-yoshi:20050903:1125702061で作ったMainクラスを実行してみると・・・
Action1.execute() Action1:Hello Action1.execute2() Action2.execute() Action2:Hello Action2.execute2() Action1.execute() Action1:Hello Action1.execute2() Action4.execute() Action4:Hello Action4.execute2()
・・・やはりバグでした(汗)id:koichikさんのTraversalクラスを参考に、コンポーネント検索部分を修正
AbstractAutoRegister
public void registAll() { S2Container root = container.getRoot(); registContainer(root); } private void registContainer(S2Container container) { for (int i = 0; i < container.getComponentDefSize(); i++) { ComponentDef cd = container.getComponentDef(i); regist(cd); } for (int i = 0; i < container.getChildSize(); i++) { registContainer(container.getChild(i)); } }
AutoRegisterInit
public void init() { S2Container root = container.getRoot(); initContainer(root); } private void initContainer(S2Container container) { for (int i = 0; i < container.getComponentDefSize(); i++) { ComponentDef cd = container.getComponentDef(i); Class componentClass = cd.getComponentClass(); if (AutoRegister.class.isAssignableFrom(componentClass)) { String name = cd.getComponentName(); AutoRegister obj = (AutoRegister) container.getComponent(name); obj.registAll(); } } for (int i = 0; i < container.getChildSize(); i++) { initContainer(container.getChild(i)); } }
修正後に再度実行
DEBUG 2005-09-04 21:35:16,265 [main] トランザクションを開始しました Action1.execute() DEBUG 2005-09-04 21:35:16,265 [main] トランザクションをコミットしました DEBUG 2005-09-04 21:35:16,265 [main] トランザクションを開始しました Action1:Hello DEBUG 2005-09-04 21:35:16,265 [main] トランザクションをコミットしました Action1.execute2() Action2.execute() DEBUG 2005-09-04 21:35:16,265 [main] トランザクションを開始しました Action2:Hello DEBUG 2005-09-04 21:35:16,265 [main] トランザクションをコミットしました Action2.execute2() DEBUG 2005-09-04 21:35:16,312 [main] トランザクションを開始しました Action1.execute() DEBUG 2005-09-04 21:35:16,312 [main] トランザクションをコミットしました DEBUG 2005-09-04 21:35:16,312 [main] トランザクションを開始しました Action1:Hello DEBUG 2005-09-04 21:35:16,312 [main] トランザクションをコミットしました Action1.execute2() DEBUG 2005-09-04 21:35:16,312 [main] トランザクションを開始しました Action4.execute() DEBUG 2005-09-04 21:35:16,312 [main] トランザクションをコミットしました Action4:Hello Action4.execute2()
ふぅ・・・上手くいきました。なんだか公開ソースチェックをやって頂いたような形になってしまって恐縮です。おかげで短期間で不安な部分の解消をすることが出来ました。