java - How to create multiple beans of same type according to configuration in Spring? -


i'm trying create specified number of beans of same type in spring.

i've tried:

@bean(name = "beanlist") public list<mybean> beanlist(         @value("${number:1}") int number         ) {     list<mybean> beanlist = new arraylist<>(number);     (int = 0; < number; i++) {         beanlist.add(new mybean());     }     return beanlist; } 

but not expected.

in way, bean "beanlist" maintained spring context, instead of it's elements, can't specify name , init method or destroy method each element in list.

any ideas?

you have @ beanfactorypostprocessor, tried following code , it's working fine, beans depends on mybean autowired:

@configuration public class appconfig implements beanfactorypostprocessor {     @override     public void postprocessbeanfactory(configurablelistablebeanfactory beanfactory) throws beansexception {         (int = 0; < 3; i++) {             system.out.println("register bean: " + i);             beanfactory.registersingleton("bean-" + i, new mybean("mybean-" + i));         }     } } 

since have complete control of creation process of mybean instance, can pass other beans in through constructor if it's necessary. hope helpful :-)


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -