Top / Java / Seasar2

コンテンツ一覧

Seasar2を使ってみる。

http://eclipse.seasar.org/updates/3.2/ から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)が自動で作成されています。見てみると

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
現在のアクセス:14636


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS