I am learning Spring and I read the Spring in Action book. I wonder if you are supposed to inject an empty arraylist (if that is possible) or create it like the example below?
public class StackOverflow {
private List<Car> cars;
public StackOverflow() {
cars = new ArrayList<Car>();
}
}
You can inject an empty list like this, however this is probably unnecessary, unless you’re trying to setup an example template spring XML config perhaps.
If you want a quick way of setting empty lists to prevent null pointer problems, then just use Collections.emptyList(). You can also do it “inline’ as follows. Note though that Collections.emptyList() returns an unmodifiable list.
You’ll also need getters and setters for cars to be able to use it from spring XML.