Android RoboGuice User Guide (6): Instance Bindings

Source: Internet
Author: User

In the previous example, Android RoboGuice User Guide (4): Linked Bindings defines a constructor without parameters when defining MyRectangle and MySquare, for example, MyRectangle:

[Java] public class MyRectangle extends Rectangle {
Public MyRectangle (){
Super (50, 50, 100,120 );
}
Public MyRectangle (int width, int height ){
Super (50, 50, width, height );
}
}
Public class MyRectangle extends Rectangle {
Public MyRectangle (){
Super (50, 50, 100,120 );
}
Public MyRectangle (int width, int height ){
Super (50, 50, width, height );
}
}

In fact, you can use Instance Bindings instead of the constructor without parameters. Instance Bindings can bind a type to a specific Instance object, it is usually used for a type that does not depend on other classes, such as various basic types, such:

[Java] bind (String. class)
. AnnotatedWith (Names. named ("jdbc url "))
. ToInstance ("jdbc: mysql: // localhost/pizza ");
Bind (Integer. class)
. AnnotatedWith (Names. named ("login timeout seconds "))
. ToInstance (10 );
Bind (String. class)
. AnnotatedWith (Names. named ("jdbc url "))
. ToInstance ("jdbc: mysql: // localhost/pizza ");
Bind (Integer. class)
. AnnotatedWith (Names. named ("login timeout seconds "))
. ToInstance (10 );

Modify the definitions of MyRectangle and MySquare as follows:

[Java] public class MySquare extends MyRectangle {
@ Inject
Public MySquare (@ Named ("width") int width ){
Super (width, width );
}
}
...
Public class MyRectangle extends Rectangle {

@ Inject
Public MyRectangle (@ Named ("width") int width,
@ Named ("height") int height ){
Super (50, 50, width, height );
}
}
Public class MySquare extends MyRectangle {
@ Inject
Public MySquare (@ Named ("width") int width ){
Super (width, width );
}
}
...
Public class MyRectangle extends Rectangle {
 
@ Inject
Public MyRectangle (@ Named ("width") int width,
@ Named ("height") int height ){
Super (50, 50, width, height );
}
}
Remove the parameter-free constructor. You can bind the int type marked as @ Named ("width") to 100 and add the following bindings:

[Java] bind (Integer. class)
. AnnotatedWith (Names. named ("width "))
. ToInstance (100 );
Bind (Integer. class)
. AnnotatedWith (Names. named ("height "))
. ToInstance (120 );
Bind (Integer. class)
. AnnotatedWith (Names. named ("width "))
. ToInstance (100 );
Bind (Integer. class)
. AnnotatedWith (Names. named ("height "))
. ToInstance (120 );
Run this example to get the same result as the previous example. When using Injector to construct a MyRectangle instance, Injector automatically selects the constructor with parameters and uses 100,120 as the width and height injection parameters. A MyRectangle object is returned to the desired location.


Although you can use Instance Bindings to map a type to a complex class Instance, RoboGuice does not recommend that you apply Instance Bindings to a complex type Instance because it slows down application startup.

The correct method is to use the @ Provides method, which will be described below.

Note: Examples in GuiceDemo do not use the list method to display all examples. To Run the required examples, you can set the Launch Activity through Run Configuration->:

 

 



Excerpted from the mobile app

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.