Seasar DI Container with AOP

S2Struts概要

S2Strutsを使うと、S2とStrutsが簡単に連動するようになります。 Actionクラスにサービスコンポーネントが自動的に設定されるので、 Actionクラスの役割は適切なサービスコンポーネントを呼び出すだけになります。 Actionクラスはコントローラに徹することで、プレゼンテーション層とサービス層の役割分担も明確になります。 また、Actionに対してAOPを適用することもできます。

S2Strutsリファレンス

動作環境とEclipseプロジェクトのセットアップ

S2と同様にJDK1.4以上が必要です。

S2StrutsVx.x.x.jarを解凍してできたs2strutsディレクトリをEclipseのJavaプロジェクトとして丸ごとインポートしてください。 .classpathを上書きするか聞かれるので、全て、はいのボタンをクリックして、全てを取り込みます。 これで、私と全く同じ開発環境になります。

サンプルは、 TomcatTomcat Pluginを使うことを前提にしています。 あらかじめインストールして置いてください。 サンプルはS2StrutsExamleVx.x.x.jarとして別途用意されているので、 ダウンロードして解凍してください。Eclipseでs2struts-exampleという名前でJavaプロジェクトを作成します。 Tomcatプロジェクトではないので気をつけてください。解凍してできたs2struts-exampleディレクトリを丸ごとインポートしてください。 .classpathを上書きするか聞かれるので、全て、はいのボタンをクリックして、全てを取り込みます。 s2struts-exampleプロジェクトを右クリックしてプロパティ->Tomcatを選びます。Tomcatプロジェクトであるをチェックし、 アプリケーションURIを/s2struts-exampleとします。これで、Tomcatを再起動して、 ブラウザからhttp://localhost:8080/s2struts-example/にアクセスすると 四則演算のサンプルを見ることができます。

基本的な使い方

まず、S2Containerを起動するためにS2StrutsServletをweb.xmlに登録する必要があります。 また、S2ContainerFilterを以下のように設定して下さい。

次に、org.apache.struts.action.ActionServletの替わりに、org.seasar.struts.servlet.S2ActionServletを、 org.apache.struts.actions.RedeployableActionServletの替わりであれば、org.seasar.struts.servlet.S2RedeployableActionServletを、登録して下さい。 Strutsオリジナルのサーブレットを使用する場合は、struts-config.xmlにorg.seasar.struts.plugin.RegistActionClassPlugInを登録して下さい。

web.xml

<web-app>
  <display-name>Struts Application</display-name>

  <filter>
    <filter-name>s2filter</filter-name>
    <filter-class>org.seasar.framework.container.filter.S2ContainerFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>s2filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <servlet>
    <servlet-name>s2container</servlet-name>
    <servlet-class>org.seasar.struts.servlet.S2StrutsServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <!-- Standard Action Servlet Configuration (with debugging) -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.seasar.struts.servlet.S2ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>s2container</servlet-name>
    <url-pattern>/s2container</url-pattern>
  </servlet-mapping>

<!-- Standard Action Servlet Mapping -->
<servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>

<!-- The Usual Welcome File List -->
<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>

<!-- Struts Tag Library Descriptors -->
<taglib> <taglib-uri>/tags/struts-bean</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib>

<taglib> <taglib-uri>/tags/struts-html</taglib-uri> <taglib-location>/WEB-INF/struts-html.tld</taglib-location> </taglib>

<taglib> <taglib-uri>/tags/struts-logic</taglib-uri> <taglib-location>/WEB-INF/struts-logic.tld</taglib-location> </taglib>

<taglib> <taglib-uri>/tags/struts-nested</taglib-uri> <taglib-location>/WEB-INF/struts-nested.tld</taglib-location> </taglib>

<taglib> <taglib-uri>/tags/struts-tiles</taglib-uri> <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location> </taglib>

</web-app>

次に、S2とStrutsを連動させるために、S2用のRequestProcessorをstruts-config.xmlに登録する必要があります。 S2RequestProcessorとS2TilesRequestProcessorを用意しています。これらは、それぞれ、Strutsの、RequestProcessorとTilesRequestProcessorに相当します。

struts-config.xml

<struts-config>
  ...
  <controller processorClass="org.seasar.struts.processor.S2RequestProcessor"/>
  ...
  <plug-in className="org.seasar.struts.plugin.RegistActionClassPlugIn"/>
  ...
</struts-config>

上記のRequestProcessorから生成される全てのActionクラスがS2と連動するようになります。 RegistActionClassPlugInを登録する事により、S2と連動するActionを、明示的にコンポーネント定義に登録しておく必要はありません(Add.dicon)。 インターフェースに対するセッターメソッドを定義しておけば、 自動的にセッター・インジェクションが行われます。 また、インターフェースのみが引数の、コンストラクタを定義している場合は、 自動的に、コンストラクタ・インジェクションが行われます。

Add.dicon

<components>
  <component class="org.seasar.struts.examples.AddServiceImpl"/>
</components>

ただし、Actionに対して、AOPの適用や、 メソッド・インジェクションを行いたい場合、 また、コンポーネントの依存関係が、型によって自動的に解決されない場合などには、 他のコンポーネントと同様に、コンポーネント定義にActionクラスを登録しておく必要があります(Multiply.dicon)。

Multiply.dicon

<components>
  <component class="org.seasar.struts.examples.MultiplyAction" instance="prototype">
    <initMethod>#out.println("inited MultiplyAction")</initMethod>
    <aspect pointcut="execute">
      <component class="org.seasar.framework.aop.interceptors.TraceInterceptor">
    </aspect>
  </component>
  <component class="org.seasar.struts.examples.MultiplyServiceImpl"/>
</components>

インスタンス属性は、任意です。このコンポーネント定義ファイルでは、では、他の演算サンプルとの比較のために、prototypeを指定しています。 singletonとするのであれば、明示的な指定の必要はありません。instance属性に、prototypeを指定をしていすると、 Actionクラスを、実装する時に、インスタンスフィールドを使用できるという利点があります。

また、これらの定義ファイルは、アプリケーション全体の定義であるapp.diconに登録する必要があります。

app.dicon

<components>
  <include path="org/seasar/struts/examples/Add.dicon"/>
  <include path="org/seasar/struts/examples/Subtract.dicon"/>
  <include path="org/seasar/struts/examples/Multiply.dicon"/>
  <include path="org/seasar/struts/examples/Divide.dicon"/>
  <include path="org/seasar/struts/examples/RequestProcessor.dicon"/>
</components>

サービスコンポーネントを受け取るためにActionクラスはコンストラクタもしくはセッターメソッドを定義しておきます。 execute()メソッドでは、サービスに処理を委譲するのみとなるので、非常にすっきりしたコードになっていることが分かると思います。

AddAction

package org.seasar.struts.examples;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class AddAction extends Action {

    private AddService addService_;

    public AddAction(AddService addService) {
        addService_ = addService;
    }

    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response) {

        CalculationForm calForm = (CalculationForm) form;
        int result = addService_.add(calForm.getArg1(), calForm.getArg2());
        calForm.setResult(result);
        return (mapping.findForward("success"));
    }
}

ActionをPOJOにした使い方

org.apache.struts.action.Actionを継承しないPOJOをActionとして使用する事が出来ます。

以下のように、インターフェースと、実装クラスを作成し実装クラスは、コンポーネント定義ファイルに定義して下さい。 アクションメソッドの戻り値は、Stringとして、forward名を返すようにして下さい。 そして、struts-configには、インターフェースをactionタグのtype属性に指定して下さい。

EchoAction

public interface EchoAction {
	String echo();
}

EchoActionImpl

public class EchoActionImpl implements EchoAction {	
    private StringForm strForm;
    
    public EchoActionImpl() {
    }

    public String echo() {
        strForm.setResult(strForm.getInput());
        
        return FowardNameConstants.SUCCESS;
    }

    public StringForm getStrForm() {
        return strForm;
    }
    public void setStrForm(StringForm strForm) {
        this.strForm = strForm;
    }
}

POJOアクションクラスに、setterメソッドがある場合、HttpServletRequestまたは、HttpSessionの値が、オートバインディングされます。
(1)HttpServletRequest#getParameter(プロパティー名)
(2)HttpServletRequest#getAttribute(プロパティー名)
(3)HttpSession#getAttribute(プロパティー名)
の優先順位で値を参照します。

また、getterメソッドがある場合も同様に、HttpServletRequestまたは、HttpSessionに値をオートバインディングします。
(1)基本的に、HttpServletRequest#setAttribute(プロパティー名,プロパティー値)を行います。
(2)HttpSession#setAttribute(プロパティー名,プロパティー値)を行いたい場合は、定数アノテーションにて、

public final static String プロパティ名_EXPORT = org.seasar.struts.util.BindingUtil.SESSION;
と記述する必要があります。

Echo.dicon

<components>
	<component class="org.seasar.struts.examples.echo.EchoActionImpl" instance="request"/>
</components>

struts-config.xml

<struts-config>
  ...
  <action-mappings>
    ...
    <action
      path="/echo"
      type="org.seasar.struts.examples.echo.EchoAction"
      name="strForm"
      scope="request"
      validate="false"
      input="/pages/echoInput.jsp">
      <forward name="success" path="/pages/strResult.jsp" />
    </action>
    ...
  <action-mappings>
  ...
</struts-config>

実装クラスのプロパティーは、web.xmlにS2ContainerFilterを登録しておく事によって、リクエストまたは、セッションとActionに対し、相互にオートバインディングされます。 リクエストとセッションのどちらにバインディングされるかは、コンポーネント定義ファイルのinstance属性により決定されます。ActionFormに関しては、struts-config.xmlに定義したscope属性により決定します。

また、インターフェースのメソッドが複数存在する場合には、org.apache.struts.actions.DispatchActionを使用する場合と同様に、 struts-config.xmlのactionタグにparameter属性の追加と、JSPのサブミットボタンに対し、name属性とvalue属性の2つの属性で、メソッドを指定して下さい。

ChangeCaseAction

public interface ChangeCaseAction {
    String toLowerCase();

    String toUpperCase();
}

struts-config.xml

<struts-config>
  ...
  <action-mappings>
    ...
    <action
      path="/changeCase"
      type="org.seasar.struts.examples.changecase.ChangeCaseAction"
      name="strForm"
      scope="request"
      validate="true"
      parameter="command"
      input="/pages/changeCaseInput.jsp">
      <forward name="success" path="/pages/strResult.jsp" />
    </action>
    ...
  <action-mappings>
  ...
</struts-config>

changeCaseInput.jsp

...
<html:submit property="command"><bean:message key="toLowerCase"/></html:submit>
<p>
<html:submit property="command"><bean:message key="toUpperCase"/></html:submit>
...

HTMLに変換されると以下のようになります。

<input type="submit" name="command" value="toLowerCase">
<p>
<input type="submit" name="command" value="toUpperCase">

ProxyActionクラスの使い方

また、ProxyActionクラスを使用して、コンポーネント定義ファイルに記述したActionを呼び出し、処理を委譲する事もできます。 これにより、struts-configとコンポーネント定義ファイルに同じクラス名を定義/同期する必要がなくなります。 struts-config内に記述しているactionのpath属性と、コンポーネント定義ファイルに定義したcomponentのname属性が一致している必要があります。 詳細は、「path属性とActionクラスのマッピングについて」を参照してください。

また、ProxyActionクラスのみに関しては、struts-configに、S2用のRequestProcessorを登録しなくとも、S2と連動する事ができます。 ただし、他のActionクラスもS2と連動させたい場合には、前述のように、S2用のRequestProcessorをstruts-config.xmlに登録する必要があります。 これは、特定のActionクラスのみを、S2と連動する為に有効である事を意味します。

struts-config.xml

<struts-config>
  ...
  <action-mappings>
    ...
    <action
      path="/subtract"
      type="org.seasar.struts.ProxyAction"
      name="calcForm"
      scope="request"
      validate="false"
      input="/pages/subtractInput.jsp">
      <forward name="success" path="/pages/result.jsp" />
    </action>
    ...
  <action-mappings>
  ...
</struts-config>

Subtract.dicon

<components>
  <component name="/subtract" class="org.seasar.struts.examples.SubtractAction"/>
  <component class="org.seasar.struts.examples.SubtractServiceImpl"/>
</components>

struts-config内に記述しているactionの、type属性を記述せずにActionクラスを指定する

ProxyActionクラスの使い方と同様に、struts-config内に記述しているactionのpath属性と、 コンポーネント定義ファイルに定義したcomponentのname属性を一致させる事によりtype属性を記述せずにActionクラスを呼び出す事が出来ます。 詳細は、「path属性とActionクラスのマッピングについて」を参照してください。 この手法を利用する為には条件があります。それは、type属性、forward属性、include属性の3つの属性が無い事です。 これにより、struts-configとコンポーネント定義ファイルに同じクラス名を定義/同期する必要がなくなります。

struts-config.xml

<struts-config>
  ...
  <action-mappings>
    ...
    <action
      path="/divide"
      name="calcForm"
      scope="request"
      validate="false"
      input="/pages/divideInput.jsp">
      <forward name="success" path="/pages/result.jsp" />
    </action>
    ...
  <action-mappings>
  ...
</struts-config>

Divide.dicon

<components>
  <component name="/divide" class="org.seasar.struts.examples.DivideAction"/>
  <component class="org.seasar.struts.examples.DivideServiceImpl"/>
</components>

path属性とActionクラスのマッピングについて

ここでは、前述の「ProxyActionクラスの使い方」と、 「struts-config内に記述しているactionの、type属性を記述せずにActionクラスを指定する」で行った、 path属性とActionクラスのマッピングについての、詳細情報を記述します。

以下の、web.xml、struts-config、そして、コンポーネント定義ファイルが存在するとします。

web.xml

<web-app>
  ...
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>config/foo</param-name>
      <param-value>/WEB-INF/struts-config-foo.xml</param-value>
    </init-param>
    ...
  </servlet>
  ...
</web-app>

struts-config.xml

<struts-config>
  ...
  <action-mappings>
    ...
    <action path="/bar"
      ...
    </action>
    ...
  <action-mappings>
  ...
</struts-config>

struts-config-foo.xml

<struts-config>
  ...
  <action-mappings>
    ...
    <action path="/baz"
      ...
    </action>
    ...
  <action-mappings>
  ...
</struts-config>

コンポーネント定義ファイル

<components>
  <component name="/bar" class="BarAction"/>
  <component name="/foo/baz" class="BazAction"/>
</components>

デフォルトのモジュール(struts-config.xmlで動作するモジュール)であれば、struts-config内に記述しているactionタグのpath属性と、 コンポーネント定義ファイル内に記述しているcomponentタグのname属性でマッピングを行います。下記のサブモジュールの(2)のパターンになります。

サブモジュール(この例では、struts-config-foo.xmlで動作するモジュール)であれば、マッピング方法が2つ存在します。

    (1)モジュール(prefix)名(/foo)+path属性(/baz)=name属性(/foo/baz
    (2)path属性(/baz)=name属性(/baz
コンポーネントを取得する優先順位は、(1)、(2)の順になります。つまり、名前が/foo/bazであるコンポーネントが見つからなければ、名前が/bazであるコンポーネントを取得します。

また、org.seasar.struts.ComponentNameCreatorをimplementsしたクラスを作成し、app.diconから登録する事で、上記のデフォルトのマッピング方法を替える事が出来ます。

適用へのヒント

サポートする機能
(1)自動的なDIのサポート
 セッター・インジェクションコンストラクタ・インジェクションを利用しての、 Actionクラスへのサービスクラスなどの自動的なDIが可能な場合
(2)インスタンス変数を使いたい場合
 コンポーネントのinstance属性をprototypeとして、Actionクラスのインスタンス変数を使いたい場合
(3)AOPを適用したい場合
 Actionから、DAOを直接利用した場面で、Actionのメソッドにトランザクションをかけたい場合など
(4)明示的な、DIが必要な場合
 メソッド・インジェクションを利用したい場合や、 セッター・インジェクション/コンストラクタ・インジェクションにて コンテナによる自動的なDIが出来ない場合


POJOAction Addの例 Multiplyの例 Subtract(ProxyAction)の例 Divideの例
(1)自動的なDIのサポート
(2)インスタンス変数を使いたい場合
(3)AOPを適用したい場合
×
(4)明示的な、DIが必要な場合
×

メリット・デメリット


メリット デメリット
POJOAction テスタビリティー向上
S2JSFなどへの移行も容易になる
Addの例 Actionクラスをコンポーネント定義ファイルに記述する必要がない サポートする機能が少ない
Multiplyの例
struts-configとコンポーネント定義ファイルに記述したActionのクラス名を同期する必要あり
(リファクタリング時、要注意)
Subtract(ProxyAction)の例 ActionMappingの情報(デフォルトは、パス名)から、コンポーネントを検索するため、struts-configとコンポーネント定義ファイルに書いてあるActionのクラス名を同期する必要なし ActionMappingの情報と、コンポーネント名を同期する必要あり
Divideの例 同上。struts-configに、type属性(Actionのクラス名)を記述する必要がないので、記述量は減る 同上。ただし、Struts的には、間違ったstruts-configの記述の仕方のため、struts-configを記述するツールとの連携が取れない可能性がある

特に理由のない場合は、POJOActionもしくは、Addの例を使用する事を推奨します。

RequestProcessorをコンポーネントから取得する

Actionクラスに共通のアスペクトを適用するのと同様の事が、RequestProcessorをコンポーネントから取得することによって可能になります。 また、アスペクトの適用以外にも他のコンポーネントと同様の使い方もすることが出来ます。

まず、S2ActionServletをweb.xmlに登録する必要があります。 web.xmlに記述するサーブレットをorg.apache.struts.action.ActionServletではなく、 org.seasar.struts.S2ActionServletもしくは、org.seasar.struts.S2RedeployableActionServletと記述してください。 (S2ActionServletは、ActionServletを継承、S2RedeployableActionServletは、RedeployableActionServletを継承したサーブレットとなっています。)

また、org.apache.struts.action.ActionServletではなく、上記2つのサーブレットを使用する場合は、struts-config.xmlにRegistActionClassPlugInを登録する必要はありません。

web.xml

<web-app>
  ...
  <!-- Standard Action Servlet Configuration (with debugging) -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.seasar.struts.S2ActionServlet</servlet-class>
    ...
  </servlet>
  ...
</web-app>

今回の例では、S2RequestProcessorにMeasureTimeInterceptorというアスペクトを適用することで、 各リクエストの処理時間を測定し標準出力しています。

RequestProcessor.dicon

<components>
  <include path="s2struts.dicon"/>
  <component class="org.seasar.struts.S2RequestProcessor">
    <aspect pointcut="process">
      <component class="org.seasar.struts.examples.MeasureTimeInterceptor">
    </aspect>
  </component>
</components>