| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Google GuiceでHelloWorld

Page history last edited by inoue.masayuki 15 years, 3 months ago

まずは、Google Guiceを使ってHello Worldを表示させたいと思います。

 

Google Guice本体の入手

本家サイトからDLしてください。

現在(2009年1月5日)では、guice-1.0.zipが最新になっています。

 

 

Google Guiceって何?

Google GuiceはGoogleが作った、DI(Dependency Injection)フレームワークです。

この「DI」というのは、既存のパターンで言ってみれば、Factoryクラスに当たります。

例えば、DAOのオブジェクトを作るために、DAOFactoryみたいなクラスを作っていたと思いますが、

DAOFactoryでやっていたのオブジェクト作成を、Google Guiceが代わりにやってくれます。

 

なにはともあれ、HelloWorldで体感してみてください。

たぶんメンドクサイと思うでしょうが、、、Struts2と組み合わせるととっても楽ちんです。

なので、今回は辛抱してください。

 

 

環境設定

Hello Worldを動作させるには、最低限以下のjarが必要です。

 

  • guice-1.0.jar

DLしたguice-1.0.zipの中にあります。

 

また、今回作るクラスは全部で5クラスになります。

  1. HelloWorldService(HelloWorldの文字列を出力するメソッドを定義するインターフェースです。)
  2. HelloWorldServiceImpl(↑の実装クラスです。)
  3. HelloWorld(HelloWorldServiceクラスをGoogle Guiceにインジェクションしてもらうクラスです。)
  4. HelloWorldModule(何のインターフェースと何のクラスが結びついているかを決めます。 今回は、HelloWorldServiceとHelloWorldServiceImplが結びついています。)
  5. HelloWorldMain(実行クラス)

 

HelloWorldService(インターフェース)の作成

まずは、HelloWorldインターフェースから作っていきましょう。

パッケージは、sample.guice.serviceとします。

クラス名は、HelloWorldServiceとします。

 

sample.guice.service.HelloWorldService.java

=== ここから ===

package sample.guice.service;

 

public interface HelloWorldService {

    public void hello();

}

=== ここまで ===

 

これはあんまり説明はいらないと思いますので、省略します。

 

 

HelloWorldServiceImplの作成

次は、先ほど作ったHelloWorldServiceインターフェースの実装クラスを作ります。

 

パッケージは、sample.guice.service.implとします。

クラス名は、HelloWorldServiceImplとします。

 

sample.guice.service.impl.HelloWorldServiceImpl.java

=== ここから ===

package sample.guice.service.impl;

 

import sample.guice.service.HelloWorldService;

 

public class HelloWorldServiceImpl implements HelloWorldService {

 

    public HelloWorldServiceImpl() {

        super();

    }

 

    public void hello() {

        System.out.println("Hello World, こんにちは、世界");

    }

}

=== ここまで ===

 

これも説明は、省略です。

 

 

HelloWorldの作成

次は、Google Guiceにインジェクションしてもらったサービスクラスを使用するクライアント的なクラスを作ります。

 

パッケージは、sample.guice.clientとします。

クラス名は、HelloWorldとします。

 

sample.guice.client.HelloWorld.java

=== ここから ===

package sample.guice.client;

 

import com.google.inject.Inject;

 

import sample.guice.service.HelloWorldService;

 

public class HelloWorld {

 

    @Inject

    private HelloWorldService helloWorldService;

 

    public HelloWorld() {

        super();

    }

 

    public void execute() {

        helloWorldService.hello();

    }

}

=== ここまで ===

 

@injectのアノテーションを記載している箇所をみてください。

HelloWorldServiceを定義しているだけです。オブジェクトは作っていません。

もし、Google Guiceが怠けていたら、executeメソッドを実行した瞬間、NullPointerExceptionで落ちてしまいますが、

実際は、そうはなりません。

 

@injectとなっている箇所に、Google Guiceが後で記載するモジュールクラスで指定しているクラスのオブジェクトを作って、放り込んでくれます。

 

 

HelloWorldModuleの作成

次は、何と何が関係を持っているのかをGoogle Guiceに教えてあげるクラスを作ります。

これは、AbstractModuleというクラスを継承して作ります。

 

パッケージは、sample.guice.moduleとします。

クラス名は、HelloWorldModuleとします。

 

sample.guice.module.HelloWorldModule.java

=== ここから ===

package sample.guice.module;

 

import sample.guice.service.HelloWorldService;

import sample.guice.service.impl.HelloWorldServiceImpl;

import com.google.inject.AbstractModule;

 

public class HelloWorldModule extends AbstractModule {

 

    public HelloWorldModule() {

        super();

    }

 

    @Override

    protected void configure() {

        bind(HelloWorldService.class).to(HelloWorldServiceImpl.class);

    }

}

=== ここまで ===

 

configureメソッドをオーバーライドしています。

見ればわかると思いますが、bind(インタフェース).to(実装クラス) となっています。

これで、HelloWorldServiceに対して、@injectで宣言されている場合、HelloWorldServiceImplのオブジェクトが作られるということです。

 

 

HelloWorldMainの作成

これで最後です。たかがHelloWorldを出力するためだけに、5つもクラス作ってきました。。。

これで、DIはなんてメンドクサイだと思わないでください。(といっても、今の段階では無理だと思いますが。。。確かにメンドクサイですからね。)

 

メインクラスですので、特に何もしませんが、Google Guiceがインジェクションしてくれたクラスを取得し、実行します。

 

パッケージは、sample.guice.mainとします。

クラス名は、HelloWorldMainとします。

 

sample.guice.main.HelloWorldMain.java

=== ここから ===

package sample.guice.main;

 

import sample.guice.client.HelloWorld;

import sample.guice.module.HelloWorldModule;

import com.google.inject.Guice;

import com.google.inject.Injector;

 

public class HelloWorldMain {

 

    public static void main(String[] args) {

        Injector injector = Guice.createInjector(new HelloWorldModule());

        HelloWorld helloWorld = injector.getInstance(HelloWorld.class);

        helloWorld.execute();

    }

}

=== ここまで ===

 

まず、Guice.createInjector(モジュールクラス)となっている箇所で、インジェクターのオブジェクトを取得します。

このインジェクターを経由して、インジェクションしてもらったクラスを取得します。

 

で、injector.getInstance(インターフェース)となっている箇所で、インジェクションしたクラスを取得しています。

ここの引数で指定しているインターフェースは、HelloWorldModuleクラスでバインドしたインターフェースです。

 

ここまで作ったファイルは以下のようになるはずです。

 

 

これを実行すると、以下のような感じになります。

 

 

 

     

今回作ったアプリをおいておきます。

 apl_google_guice.zip

 

Comments (0)

You don't have permission to comment on this page.