Injector creates an instance object of a type by examining the bindings definition. The bindings defined in module are called explicit declaration bindings (Explicit bindings). Injector first creates an instance object for a type with explicit bindings. When a type does not explicitly define a binding, Injector attempts to construct the instant binding (Just-in-time bindings), and the JIT bindings becomes an implied binding (implicit bindings).
Eligible constructor
Injector creates an instance object of the class by using the injectable constructor of the class. Injectable constructor a constructor that can be defined for the class that has no arguments, or a constructor with a @injector tag.
Like the Android Roboguice use Guide (4): linked constructor MyRectangle in bindings:
public class
MyRectangle extends rectangle{public
myrectangle () {
super (50,50,100,120);
}
...
}
and Android Roboguice guidelines for use (6): Instance bindings-defined constructors with @Injector tags:
public class MySQUARE extends
myrectangle {
@Inject public mysquare (@Named ("width") int width) {
super ( width,width);
}
@ImplementedBy
The tag notifies injector of the default implementation of a type, which functions like linked bindings, for example:
@ImplementedBy
(paypalcreditcardprocessor.class) public
interface Creditcardprocessor {
Chargeresult Charge (String amount, CreditCard CreditCard)
throws unreachableexception;}
And
Bind
(Creditcardprocessor.class). to
(Paypalcreditcardprocessor.class);
Equivalent. If a type contains both @ImplementedBy and bind definitions, the definition in bind will be used preferentially.
Note: @ImplementedBy defines dependencies from interface to implementations and is generally not recommended for use.
@ProvidedBy
@ProvidedBy notifies injector that a type uses that default provider to create an instance object, for example:
@ProvidedBy
(databasetransactionlogprovider.class) public
interface TransactionLog {
void Logconnectexception (unreachableexception e);
void Logchargeresult (chargeresult result);
}
Equivalent to the following binding:
Bind (Transactionlog.class)
. Toprovider (Databasetransactionlogprovider.class);
As with @ImplementedBy, if @providedby and bind are defined at the same time, the bind defined in the module is preferred
View a full set of articles: Http://www.bianceng.cn/OS/extra/201301/34950.htm