To inject a parameterized type, such as List <String>:
[Java] class Example {
@ Inject
Void setList (List <String> list ){
...
}
}
Class Example {
@ Inject
Void setList (List <String> list ){
...
}
}
You can use TypeLiteral to create this binding. TypeLiteral is a special type that can be used to represent parameterized types.
[Java] @ Override public void configure (){
Bind (new TypeLiteral <List <String >> (){})
. ToInstance (new ArrayList <String> ());}
@ Override public void configure (){
Bind (new TypeLiteral <List <String >> (){})
. ToInstance (new ArrayList <String> ());}
Or use the @ Provides method:
[Java] @ Provides List <String> providesListOfString (){
Return new ArrayList <String> ();
}
@ Provides List <String> providesListOfString (){
Return new ArrayList <String> ();
}
So far, I have introduced the usage of Google Guice, which is also applicable to Java SE and Java EE platforms. For more information, see the English documentation, next we will introduce the Dependency Injection (Roboguice) usage related to the Android platform.
Excerpted from the mobile app