I would like dynamically register multiple objects as Spring beans. Is is possible, without BeanFactoryPostProcessor
?
@Configuration public class MyConfig {
@Bean A single() { return new A("X");}
@Bean List<A> many() { return Arrays.asList(new A("Y"), new A("Z"));}
private static class A {
private String name;
public A(String name) { this.name = name;}
@PostConstruct public void print() {
System.err.println(name);
}
}
}
Actual output shows only one bean is working:
X
Expected:
X Y Z
Spring 4.3.2.RELEASE
What I want is impossible but requested with https://jira.spring.io/browse/SPR-13348
If you think multiple bean registration is OK please upvote
You should specify your
A
bean definition kind of prototype with a parameterprototype
scope allows Spring to create a newA
with everytemplate
execution and register an instance into context.The result of
main
execution is as you expect