WebWork2 with S2

WebWork2にS2を組み込んでみました。
ここにソースはあります(http://www.mobster.jp/webwork/)

WebWork2のexternalRef機能を使うと外部のIoCコンテナと連携ができるようになるのですが、WW2の設定(xwork.xml)とIoCの設定2つを同期管理する必要がでてきます。これはめんどくさい。
S2はType3をサポートしているので、S2用のアダプターを作ることでうまく融合できました。
アクションの生成はこんな感じです。

(1)アクションの生成時にWW2の設定より「アクションのClassオブジェクト」取得してS2に登録。
(2)S2にアクションのインスタンスを生成させて返却。

これでActionのコンストラクタにS2管理下のコンポーネントをセットすることができます。

Actionのコードはこんなにすっきり簡単です。
ストラッチよりテスタビリティよさげじゃないですか>id:m-hashimotoくん

public interface Counter {
  int getCount();
}
public class CounterImpl implements Counter {
  public int getCount() {
    return 999;
  }
}
public class CounterAction implements Action {
  private Counter counter;
  public CounterAction(Counter counter) {  // 実際はCounterImplがセットされる。
    this.counter = counter;
  }
  public String execute() throws Exception {
    return SUCCESS;
  }
  public int getCount() {
    return counter.getCount();
  }
}

設定はこんな感じ

[S2-config.xml]

  


[xwork.xml]

  
    counter.jsp