Injector checks the bindings definition to create an instance object of a certain type. The binding defined in the Module is called "Explicit bindings ". Injector will first create an instance object for a type with Explicit Bindings. When binding is not explicitly defined for a type, Injector tries to construct the "Just-in-time Bindings" and JIT Bindings also becomes the implicit bindings ).
Eligible Constructor
Injector creates instance objects of the class by using injectable constructor of the class. Injectable constructor can be a public constructor defined for this class without parameters or a constructor marked with @ Injector.
For example, the Android RoboGuice User Guide (4): Linked Bindingshttp: // www.bkjia.com/kf/201205/130094.html has no unique structure of myrectangle:
[Java] public class MyRectangle extends Rectangle {
Public MyRectangle (){
Super (50, 50, 100,120 );
}
...
}
Public class MyRectangle extends Rectangle {
Public MyRectangle (){
Super (50, 50, 100,120 );
}
...
}
And Android RoboGuice User Guide (6): Instance Bindingshttp: // www.bkjia.com/kf/201205/130096.html:
[Java] public class MySquare extends MyRectangle {
@ Inject public MySquare (@ Named ("width") int width ){
Super (width, width );
}
}
Public class MySquare extends MyRectangle {
@ Inject public MySquare (@ Named ("width") int width ){
Super (width, width );
}
}
[Java] @ ImplementedBy
This flag notifies Injector of the default implementation of a type. Its function is similar to that of Linked Bindings. For example:
@ ImplementedBy
This flag notifies Injector of the default implementation of a type. Its function is similar to that of Linked Bindings. For example:
[Java] @ ImplementedBy (PayPalCreditCardProcessor. class)
Public interface CreditCardProcessor {
ChargeResult charge (String amount, CreditCard creditCard)
Throws UnreachableException ;}
@ ImplementedBy (PayPalCreditCardProcessor. class)
Public interface CreditCardProcessor {
ChargeResult charge (String amount, CreditCard creditCard)
Throws UnreachableException;} [java] view plaincopyprint? And
And
[Java] bind (CreditCardProcessor. class). to (PayPalCreditCardProcessor. class );
Bind (CreditCardProcessor. class). to (PayPalCreditCardProcessor. class );
Equivalent. If a type contains both @ ImplementedBy and bind definitions, the bind definition is used first.
Note: @ ImplementedBy defines the dependency from Interface to implementation. It is generally not recommended.
@ ProvidedBy
@ ProvidedBy notifies Injector of the default Provider used to create instance objects of a certain type. For example:
[Java] @ ProvidedBy (DatabaseTransactionLogProvider. class)
Public interface TransactionLog {
Void logConnectException (UnreachableException e );
Void logChargeResult (ChargeResult result );
}
@ ProvidedBy (DatabaseTransactionLogProvider. class)
Public interface TransactionLog {
Void logConnectException (UnreachableException e );
Void logChargeResult (ChargeResult result );
}
It is equivalent to the following Binding:
[Java] bind (TransactionLog. class)
. ToProvider (DatabaseTransactionLogProvider. class );
Bind (TransactionLog. class)
. ToProvider (DatabaseTransactionLogProvider. class );
Like @ ImplementedBy, if both @ ProvidedBy and bind are defined, bind defined in the module takes precedence.
Excerpted from the mobile app