Android Roboguice Usage Guide (Just-in-time) bindings

Source: Internet
Author: User
Tags definition bind constructor

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

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.