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