Top / Java / Seasar2

コンテンツ一覧

Seasar2を使ってみる。

http://eclipse.seasar.org/updates/3.3/ からDoltengをダウンロードしてEclipseにインストールしておきます。Javaプロジェクトを作るときに必要なライブラリをコピーしてくれて便利ですね。

やってみる

まずはEclipseから Dolteng Projectを作成します。

Root Package Name: nu.mine.kino.s2sample
Application Type: Standalone Application

などを入力してプロジェクトを作成します。

では実際にやってみます。

package nu.mine.kino.s2sample.service;

public interface HogeService {
  void execute();
}
package nu.mine.kino.s2sample.service.impl;

import nu.mine.kino.s2sample.service.HogeService;

public class HogeServiceImpl implements HogeService {
  public void execute() {
    System.out.println("execute.");
  }
}
import nu.mine.kino.s2sample.service.HogeService;

import org.seasar.framework.container.SingletonS2Container;
import org.seasar.framework.container.factory.SingletonS2ContainerFactory;

public class Main {
  public static void main(String[] args) {
    System.out.println("Start.");
    SingletonS2ContainerFactory.init();
    HogeService hoge = SingletonS2Container.getComponent(HogeService.class);
    hoge.execute();
    System.out.println("End.");
  }
}

こういう感じで実装を隠蔽しつつインタフェースベースでプログラミングすることができるわけですね。

diconファイル。

さてSeasarはSpringとちがってできるだけxmlを書かずに自動化(Coc:Convention over Configuration:設定より規約)する思想で作られてるようで、先のインタフェースHogeService? と実装 HogeServiceImpl?も自動的にコンテナから取得することができました。

ちなみにDoltengをつかうといくつかのxmlファイル(*.dicon)が自動で作成されています。見てみると

  • app.dicon
    <components>
      <include path="convention.dicon"/>
      <include path="aop.dicon"/>
    </components>
  • convention.dicon
    <components>
    	<component class="org.seasar.framework.convention.impl.NamingConventionImpl">
    		<initMethod name="addRootPackageName">
    			<arg>"nu.mine.kino.s2sample"</arg>
    		</initMethod>
    	</component>
    </components>
  • creator.dicon
    <components>
      <include path="customizer.dicon"/>
      <include path="convention.dicon"/>
      <component name="actionCreator" class="org.seasar.framework.container.creator.ActionCreator"/>
      <component name="converterCreator" class="org.seasar.framework.container.creator.ConverterCreator"/>
      <component name="daoCreator" class="org.seasar.framework.container.creator.DaoCreator"/>
      <component name="dtoCreator" class="org.seasar.framework.container.creator.DtoCreator"/>
      <component name="dxoCreator" class="org.seasar.framework.container.creator.DxoCreator"/>
      <component name="helperCreator" class="org.seasar.framework.container.creator.HelperCreator"/>
      <component name="interceptorCreator" class="org.seasar.framework.container.creator.InterceptorCreator"/>
      <component name="logicCreator" class="org.seasar.framework.container.creator.LogicCreator"/>
      <component name="pageCreator" class="org.seasar.framework.container.creator.PageCreator"/>
      <component name="serviceCreator" class="org.seasar.framework.container.creator.ServiceCreator"/>
      <component name="validatorCreator" class="org.seasar.framework.container.creator.ValidatorCreator"/>
    </components>
  • customizer.dicon
    <components>
      <include path="default-customizer.dicon"/>
    </components>
  • hotdeploy.dicon
    <components>
      <include path="convention.dicon"/>
      <include path="customizer.dicon"/>
      <include path="creator.dicon"/>
      <component class="org.seasar.framework.container.hotdeploy.HotdeployBehavior"/>
    </components>    コイツはs2-framework-2.4.34.jar に入ってる
  • s2container.dicon
    <components>
      <include condition="#ENV == 'ut'" path="warmdeploy.dicon"/>
      <include condition="#ENV == 'ct'" path="hotdeploy.dicon"/>
      <include condition="#ENV != 'ut' and #ENV != 'ct'" path="cooldeploy.dicon"/>
    </components>
    などが作られてました。

mainクラスでは

SingletonS2ContainerFactory.init();
HogeService hoge = SingletonS2Container.getComponent(HogeService.class);

として特に設定ファイル名などは指定してないのですが、その場合はapp.diconをクラスパスから検索して自動的にconfigureがされるようです。app.diconには

<include path="convention.dicon"/>
<include path="aop.dicon"/>

って書いてあって、convention.dicon をincludeせよってなってます。こんな感じに芋づる式に設定ファイルが読み込まれていくみたいです。

関連リンク


FrontPage
現在のアクセス:14614


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2009-05-29 (金) 12:33:24 (5446d)