第二章 Guice快速上手:Guice指导教程(原创翻译)

目录
[隐藏]

如何使用Guice实现依赖注入?

How to start doing dependency injection with Guice.

快速上手 Getting Started

在构建一个对象时,如果通过构造方法传参的方式实现依赖注入,首先要创建它所依赖的对象。但是,在创建这些对象时,可能又需要为各自创建所依赖的对象(A依赖B,B又依赖C)。所以当你构建一个对象时,会形成一个对象的关系依赖图。

With dependency injection, objects accept dependencies in their constructors. To construct an object, you first build its 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.

实现这个对象关系图也不容易——他们之间错中复杂,容易出错并且不容易测试。为了避免这个情况,使用Guice能主动为你构造这个对象关系图。当然,为了实现这个目的,Guice需要先按照你的需求进行进行配置。

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.

举个例子:我们在BillingService类的构造方法中传入了所依赖的两个参数:CreditCardProcessor和TransactionLog。为了BillingService的构造方法能被Guice识别调用,需要在构造方法上加一个@Inject注解:

To illustrate, we’ll start the BillingService class that accepts its dependent interfaces CreditCardProcessor and TransactionLog in its constructor. To make it explicit that the BillingService constructor is invoked by Guice, we add the @Inject annotation:

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

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

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

现在,我们用PaypalCreditCardProcessor和DatabaseTransactionLog构建了BillingService(即:BillingService构造方法中需要传入参数PaypalCreditCardProcessor和DatabaseTransactionLog)。Guice使用绑定类型来映射实现。一个module类便是一系列绑定关系的集合,调用语法像说英语一样简单:

We want to build a BillingService 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:

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

     /*
      * This tells Guice that whenever it sees a dependency on a TransactionLog,
      * it should satisfy the dependency using a DatabaseTransactionLog.
      */
    bind(TransactionLog.class).to(DatabaseTransactionLog.class);

     /*
      * Similarly, this binding tells Guice that when CreditCardProcessor is used in
      * a dependency, that should be satisfied with a PaypalCreditCardProcessor.
      */
    bind(CreditCardProcessor.class).to(PaypalCreditCardProcessor.class);
  }
}

这些module类是注入器(injector)的组成元件,也就是Guice形成的关系图。接下来,我们创建一个注入器(injector),这样就可以用它来构建我们的BillingService对象了。

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 BillingService:

public static void main(String[] args) {
    /*
     * Guice.createInjector() takes your Modules, and returns a new Injector
     * instance. Most applications will call this method exactly once, in their
     * main() method.
     */
    Injector injector = Guice.createInjector(new BillingModule());

    /*
     * Now that we've got the injector, we can build objects.
     */
    BillingService billingService = injector.getInstance(BillingService.class);
    ...
  }

通过构建billingService对象,Guice也创建了一个简单的对象关系图。这个“图”包含了记账服务和它所依赖的Pos机以及日志记录系统间的关系。    

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.


下一章:Guice 绑定方式 Bindings


说明:

      鉴于网上guice中文资料较少,出于个人爱好,对该项目下的用户API文档进行翻译。如有翻译不恰当之处,还望指正。

      google Guice 项目地址:https://github.com/google/guice

      Guice 英文API地址:https://github.com/google/guice/wiki/Motivation

分享到:

发表评论

昵称

沙发空缺中,还不快抢~