在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Android/ Just-in-time Bindings
Just-in-time Bindings
Binding Annotations
Standard Injection
第一個(gè)例子 Hello World
Bindings 概述
Linked Bindings
如何綁定 generic 類型
@Provides Methods
RoboGuice 功能描述
概述
綜合示例 Astroboy
Inject Resources
Instance Bindings
Inject 自定義 View
Scopes
Provider Bindings
Untargetted Bindings
Inject Extra
第一個(gè)例子 Hello World
Inject Context
發(fā)送接收 Events
Inject View

Just-in-time Bindings

Injector 通過(guò)檢查 bindings 定義來(lái)創(chuàng)建某個(gè)類型的實(shí)例對(duì)象。定義在 Module 中的綁定稱為“明確聲明綁定(Explicit bindings”。Injector 會(huì)首先使用帶有 Explicit Bindings 為某個(gè)類型創(chuàng)建實(shí)例對(duì)象。 當(dāng)?shù)硞€(gè)類型沒(méi)有明確定義綁定時(shí),Injector 試圖構(gòu)造“即時(shí)綁定 (Just-in-time Bindings),JIT Bindings 也成為隱含綁定 (implicit bindings).

Eligible Constructor

Injector 通過(guò)使用類的 injectable constructor 來(lái)創(chuàng)建該類的實(shí)例對(duì)象。injectable constructor 可以為該類定義的 public 不帶參數(shù)的構(gòu)造函數(shù)或是帶有 @Injector 標(biāo)記的構(gòu)造函數(shù)。

比如 Android RoboGuice 使用指南(4):Linked Bindings 中 MyRectangle 的無(wú)參數(shù)構(gòu)造函數(shù):

public class MyRectangle extends Rectangle{
 public MyRectangle(){
 super(50,50,100,120);
 }
 ...
}

Android RoboGuice 使用指南(6):Instance Bindings 定義的含 @Injector 標(biāo)記的構(gòu)造函數(shù):

public class MySquare extends MyRectangle {
 @Inject public MySquare(@Named("width") int width){
 super(width,width);
 }
}

@ImplementedBy

該標(biāo)記通知 Injector 某個(gè)類型的缺省實(shí)現(xiàn),其功能和 Linked Bindings 類似,例如:

@ImplementedBy(PayPalCreditCardProcessor.class)
public interface CreditCardProcessor {
 ChargeResult charge(String amount, CreditCard creditCard)
 throws UnreachableException; }

bind(CreditCardProcessor.class)
 .to(PayPalCreditCardProcessor.class);

等效。 如果某個(gè)類型同時(shí)含有 @ImplementedBy 和 bind 定義,將優(yōu)先使用 bind 中的定義。

注: @ImplementedBy 定義了從 Interface 到實(shí)現(xiàn)的依賴,一般不建議使用。

@ProvidedBy

@ProvidedBy 通知 Injector 某個(gè)類型使用那個(gè)缺省 Provider 來(lái)創(chuàng)建實(shí)例對(duì)象,例如:

@ProvidedBy(DatabaseTransactionLogProvider.class)
public interface TransactionLog {
 void logConnectException(UnreachableException e);
 void logChargeResult(ChargeResult result);
}

和下面 Binding 等效:

bind(TransactionLog.class)
 .toProvider(DatabaseTransactionLogProvider.class);

和 @ImplementedBy 一樣,如果同時(shí)定義了 @ProvidedBy 和 bind,模塊中定義的 bind 優(yōu)先