GIN: inject an array of objects
开发者_如何学编程Is there a way to inject an array of objects or collection of objects?
@Inject
private A[] objects
How do I create bindings for this case in my GinModule?
Try this:
public class YourModul extends AbstractGinModule {
@Provides
public List<Integer> getIntegers() {
final List<Integer> integers = new ArrayList<Integer>();
integers.add(Integer.valueOf(1));
integers.add(Integer.valueOf(2));
integers.add(Integer.valueOf(3));
return integers;
}
}
The list of integers can now be injected as usual:
public class YouGinClass {
@Inject
private List<Integer> integers;
}
The getIntegers()
is invoked everytime when a list of integer should be injected.
精彩评论