Android RoboGuice User Guide (10): Just-in-time Bindings

Source: Internet
Author: User

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
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.