第三章 【第五节】 ProviderBindings:Guice指导教程(原创翻译)

ProviderBindings

提供者绑定 Provider Bindings

当你使用@Provides方法越来越复杂的时候,我们可以考虑分别移到各自的类里。一个提供者的类(provider类)实现了一个Provider接口,可以更简单的获取值:

When your @Provides methods start to grow complex, you may consider moving them to a class of their own. The provider class implements Guice’s Provider interface, which is a simple, general interface for supplying values:

public interface Provider<T> {
  T get();
}

提供者的实现有自身的依赖,通过@Inject注解标注在构造方法上实现。它实现Provider接口,定义了更复杂类型的返回类型。
Our provider implementation class has dependencies of its own, which it receives via its @Inject-annotated constructor. It implements the Provider interface to define what’s returned with complete type safety:

public class DatabaseTransactionLogProvider implements Provider<TransactionLog> {
  private final Connection connection;

  @Inject
  public DatabaseTransactionLogProvider(Connection connection) {
    this.connection = connection;
  }

  public TransactionLog get() {
    DatabaseTransactionLog transactionLog = new DatabaseTransactionLog();
    transactionLog.setConnection(connection);
    return transactionLog;
  }
}

最后,我们用.toProvider语句进行绑定
Finally we bind to the provider using the .toProvider clause:

public class BillingModule extends AbstractModule {
  @Override
  protected void configure() {
    bind(TransactionLog.class)
        .toProvider(DatabaseTransactionLogProvider.class);
  }

如果你的提供者比较复杂,记得要测试一下哦。
If your providers are complex, be sure to test them!

抛异常 Throwing Exceptions

Guice不允许从Providers中抛出异常。提供者(Provider)接口不允许检测异常并抛出。RuntimeExceptions异常可能包装在ProvisionException或者CreationException里,并且防止创建注入器。如果你需要抛出异常的话,可以使用ThrowingProviders拓展。
Guice does not allow exceptions to be thrown from Providers. The Provider interface does not allow for checked exception to be thrown. RuntimeExceptions may be wrapped in a ProvisionException or CreationException and may prevent your Injector from being created. If you need to throw an exception for some reason, you may want to use the ThrowingProviders extension.


实例

ProviderBindings(提供者绑定):就是实现Provider接口来生产依赖的对象。

public interface Animal {
    void say();
}
 
public class Cat implements Animal {
     
    String name;
     
    public void setName(String name) {
        this.name = name;
    }
     
    @Override
    public void say() {
        System.out.println("i am a cat");
    }
     
    @Override
    public String toString() {
        return "name==>" + this.name;
    }
}
 
import com.google.inject.AbstractModule;
 
public class AnimalModule extends AbstractModule {
 
    @Override
    protected void configure() {
        bind(String.class).toInstance("Tom");
        bind(Animal.class).toProvider(AnimalProvider.class);
        System.out.println("run configure");
    }
}
 
import com.google.inject.Inject;
import com.google.inject.Provider;
 
public class AnimalProvider implements Provider<Animal>{
 
    String name;
     
    @Inject
    public AnimalProvider(String name) {
        this.name = name;
    }
     
    public Animal get() {
        Cat cat = new Cat();
        cat.setName(name);
        System.out.println("run Provider.get()");
        return cat;
    }
}
 
import com.google.inject.Guice;
import com.google.inject.Injector;
 
public class Test {
    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new AnimalModule());
        AnimalProvider provider = injector.getInstance(AnimalProvider.class);
        Animal cat = provider.get();
        System.out.println(cat);
        cat.say();
    }
}

执行结果:
run AnimalModule.configure()
run Provider.get()
name==>Tom
i am a cat

下一节:UntargettedBindings


说明:

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

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

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

分享到:

发表评论

昵称

沙发空缺中,还不快抢~