I would like to create dynamic list of Spring managed beans in my @Configuration annotated AppConfig:
@Bean
List<SomeObject> createSomeObjects(@Value("${object.count}") final int objectCount) {
List<Object> objects = new ArrayList<>();
for (int i = 1; i <= objectCount; i++) {
objects.add(new SomeObject())
}
return objects;
}
Then, I would like to inject this List of objects into another bean like this:
@Autowired
List<SomeObject> objects;
What I am getting is the following exception:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: java.util.List ..
I could create some sort of container for my object list, but am looking for a more elegant solution.
Spring handles collections specially, and this just doesn't work; the container always interprets a collection as the group of individually declared beans. After some discussion and modifications it will work in 4.3, but for now, the best solution is to use a holder class.