Locked History Actions

Diff for "guice/Manual/UserGuide/GettingStarted"

Differences between revisions 1 and 2
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
With dependency injection, objects accept dependencies in their constructors. To construct an object, you first build it's dependencies. But to build each dependency, you need its dependencies, and so on. So when you build an object, you really need to build an object graph.
Line 4: Line 3:
Building object graphs by hand is labour intensive, error prone, and makes testing difficult. Instead, Guice can build the object graph for you. But first, Guice needs to be configured to build the graph exactly as you want it. 依存性注入により、オブジェクトは依存性をコンストラクタ引数として受け取る。
オブジェクトを作成するにあたっては、まずその依存性をビルドする必要がある。
しかし、そのれらの依存性をビルドするには、そのまた依存性が必要になるのである。
というように永遠に続く。
したがって、あるオブジェクトをビルドするには、オブジェクトグラフのビルドが必要になるのである。
Line 6: Line 9:
To illustrate, we'll start the RealBillingService class that accepts its dependent interfaces CreditCardProcessor and TransactionLog in its constructor. To make it explicit that the RealBillingService constructor is invoked by Guice, we add the @Inject annotation: 手作業でオブジェクトグラフをビルドすることは、かなり大変でエラーも出やすく、そしてテストも難しくなる。
その代わりにGuiceはオブジェクトグラフを代わりに作ってくれる。
しかし最初に、Guiceが正確なグラフを作れるように構成しておかなくてはいけない。

我々は、CreditCardProcessorとTransactionLogをコンストラクタ引数として受け取るRealBillingServiceクラスを例としてあげた。
RealBillingServiceのコンストラクタがGuiceによって起動されるように、@Injectアノテーションをつけた。
Line 25: Line 34:
We want to build a RealBillingService using PaypalCreditCardProcessor and DatabaseTransactionLog. Guice uses bindings to map types to their implementations. A module is a collection of bindings specified using fluent, English-like method calls:
PaypalCreditCardProcessorとDatabaseTransactionLogを使って、RealBillingServiceをビルドしたい。
Guiceは型をその実装にマップする。
Moduleはバインディングのコレクションであり、英文のようなメソッドコールによってそのバインディングが指定される。
Line 32: Line 44:
      * This tells Guice that whenever it sees a dependency on a TransactionLog,
      * it should satisfy the dependency using a DatabaseTransactionLog.
      * TransactionLogがあったら、その依存性をDatabaseTransactionLogを使って解決しろと
      * Guiceに伝える。
Line 38: Line 50:
      * Similarly, this binding tells Guice that when CreditCardProcessor is used in
      * a dependency, that should be satisfied with a PaypalCreditCardProcessor.
      * 同様にCreditCardProcessorをPaypalCreditCardProcessorで解決しろと
      * Guiceに伝える。
Line 45: Line 57:
The modules are the building blocks of an injector, which is Guice's object-graph builder. First we create the injector, and then we can use that to build the RealBillingService:
モジュールはインジェクタのビルディングブロックであり、インジェクタはGuiceのオブジェクトグラフビルダである。
まず最初にインジェクタを作成し、RealBillingServiceをビルドすることにしよう。
Line 49: Line 63:
     * Guice.createInjector() takes your Modules, and returns a new Injector
     * instance. Most applications will call this method exactly once, in their
     * main() method.
     * Guice.createInjector()はあなたのモジュールを受け取り、新しいインジェクタを返す。
     * 大抵のアプリでは、このメソッドコールは一度だけmain()メソッドの中で行われる。
Line 56: Line 69:
     * Now that we've got the injector, we can build objects.      * インジェクタを取得したので、オブジェクトをビルドしよう。
Line 62: Line 75:
By building the billingService, we've constructed a small object graph using Guice. The graph contains the billing service and its dependent credit card processor and transaction log.
billingServiceをビルドすることにより、Guiceを使って小さなオブジェクトグラフを作成することになる。
このグラフには、Billing Serviceとその依存であるCredit Card ProcessorとTransaction Logが含まれる。

始め方

依存性注入により、オブジェクトは依存性をコンストラクタ引数として受け取る。 オブジェクトを作成するにあたっては、まずその依存性をビルドする必要がある。 しかし、そのれらの依存性をビルドするには、そのまた依存性が必要になるのである。 というように永遠に続く。 したがって、あるオブジェクトをビルドするには、オブジェクトグラフのビルドが必要になるのである。

手作業でオブジェクトグラフをビルドすることは、かなり大変でエラーも出やすく、そしてテストも難しくなる。 その代わりにGuiceはオブジェクトグラフを代わりに作ってくれる。 しかし最初に、Guiceが正確なグラフを作れるように構成しておかなくてはいけない。

我々は、CreditCardProcessorTransactionLogをコンストラクタ引数として受け取るRealBillingServiceクラスを例としてあげた。 RealBillingServiceのコンストラクタがGuiceによって起動されるように、@Injectアノテーションをつけた。

class RealBillingService implements BillingService {
  private final CreditCardProcessor processor;
  private final TransactionLog transactionLog;

  @Inject
  RealBillingService(CreditCardProcessor processor, 
      TransactionLog transactionLog) {
    this.processor = processor;
    this.transactionLog = transactionLog;
  }

  @Override
  public Receipt chargeOrder(PizzaOrder order, CreditCard creditCard) {
    ...
  }
}

PaypalCreditCardProcessorDatabaseTransactionLogを使って、RealBillingServiceをビルドしたい。 Guiceは型をその実装にマップする。 Moduleはバインディングのコレクションであり、英文のようなメソッドコールによってそのバインディングが指定される。

public class BillingModule extends AbstractModule {
  @Override 
  protected void configure() {

     /*
      * TransactionLogがあったら、その依存性をDatabaseTransactionLogを使って解決しろと
      * Guiceに伝える。
      */
    bind(TransactionLog.class).to(DatabaseTransactionLog.class);

     /*
      * 同様にCreditCardProcessorをPaypalCreditCardProcessorで解決しろと
      * Guiceに伝える。
      */
    bind(CreditCardProcessor.class).to(PaypalCreditCardProcessor.class);
  }
}

モジュールはインジェクタのビルディングブロックであり、インジェクタはGuiceのオブジェクトグラフビルダである。 まず最初にインジェクタを作成し、RealBillingServiceをビルドすることにしよう。

 public static void main(String[] args) {
    /*
     * Guice.createInjector()はあなたのモジュールを受け取り、新しいインジェクタを返す。
     * 大抵のアプリでは、このメソッドコールは一度だけmain()メソッドの中で行われる。
     */
    Injector injector = Guice.createInjector(new BillingModule());

    /*
     * インジェクタを取得したので、オブジェクトをビルドしよう。
     */
    RealBillingService billingService = injector.getInstance(RealBillingService.class);
    ...
  }

billingServiceをビルドすることにより、Guiceを使って小さなオブジェクトグラフを作成することになる。 このグラフには、Billing Serviceとその依存であるCredit Card ProcessorとTransaction Logが含まれる。